1741D.md (2940B)
1 Source: https://codeforces.com/problemset/problem/1741/D 2 3 D. Masha and a Beautiful Tree 4 5 time limit per test 6 7 2 seconds 8 9 memory limit per test 10 11 256 megabytes 12 13 input 14 15 standard input 16 17 output 18 19 standard output 20 21 The girl named Masha was walking in the forest and found a complete binary tree of height $n$ and a permutation $p$ of length $m=2^n$. 22 23 A complete binary tree of height $n$ is a rooted tree such that every vertex except the leaves has exactly two sons, and the length of the path from the root to any of the leaves is $n$. The picture below shows the complete binary tree for $n=2$. 24 25 A permutation is an array consisting of $n$ different integers from $1$ to $n$. For example, [$2,3,1,5,4$] is a permutation, but [$1,2,2$] is not ($2$ occurs twice), and [$1,3,4$] is also not a permutation ($n=3$, but there is $4$ in the array). 26 27 Let's enumerate $m$ leaves of this tree from left to right. The leaf with the number $i$ contains the value $p_i$ ($1 \le i \le m$). 28 29 For example, if $n = 2$, $p = [3, 1, 4, 2]$, the tree will look like this: 30 31  32 33 Masha considers a tree beautiful if the values in its leaves are ordered from left to right in increasing order. 34 35 In one operation, Masha can choose any non-leaf vertex of the tree and swap its left and right sons (along with their subtrees). 36 37 For example, if Masha applies this operation to the root of the tree discussed above, it will take the following form: 38 39  40 41 Help Masha understand if she can make a tree beautiful in a certain number of operations. If she can, then output the minimum number of operations to make the tree beautiful. 42 43 Input 44 45 The first line contains single integer $t$ ($1 \le t \le 10^4$) — number of test cases. 46 47 In each test case, the first line contains an integer $m$ ($1 \le m \le 262144$), which is a power of two — the size of the permutation $p$. 48 49 The second line contains $m$ integers: $p_1, p_2, \dots, p_m$ ($1 \le p_i \le m$) — the permutation $p$. 50 51 It is guaranteed that the sum of $m$ over all test cases does not exceed $3 \cdot 10^5$. 52 53 Output 54 55 For each test case in a separate line, print the minimum possible number of operations for which Masha will be able to make the tree beautiful or -1, if this is not possible. 56 57 Example 58 59 Input 60 61 ``` 62 4 63 64 8 65 66 6 5 7 8 4 3 1 2 67 68 4 69 70 3 1 4 2 71 72 1 73 74 1 75 76 8 77 78 7 8 4 3 1 2 6 5 79 ``` 80 81 Output 82 83 ``` 84 4 85 -1 86 0 87 -1 88 ``` 89 90 Note 91 92 Consider the first test. 93 94 In the first test case, you can act like this (the vertex to which the operation is applied at the current step is highlighted in purple): 95 96 | | | 97 | --- | --- | 98 | | | 99 | | | 100 101 It can be shown that it is impossible to make a tree beautiful in fewer operations. 102 103 In the second test case, it can be shown that it is impossible to make a tree beautiful. 104 105 In the third test case, the tree is already beautiful.