D. Masha and a Beautiful Tree

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

The girl named Masha was walking in the forest and found a complete binary tree of height nn and a permutation pp of length m=2nm=2n.

A complete binary tree of height nn 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 nn. The picture below shows the complete binary tree for n=2n=2.

A permutation is an array consisting of nn different integers from 11 to nn. For example, [2,3,1,5,42,3,1,5,4] is a permutation, but [1,2,21,2,2] is not (22 occurs twice), and [1,3,41,3,4] is also not a permutation (n=3n=3, but there is 44 in the array).

Let's enumerate mm leaves of this tree from left to right. The leaf with the number ii contains the value pipi (1≤i≤m1≤i≤m).

For example, if n=2n=2, p=[3,1,4,2]p=[3,1,4,2], the tree will look like this:

Masha considers a tree beautiful if the values in its leaves are ordered from left to right in increasing order.

In one operation, Masha can choose any non-leaf vertex of the tree and swap its left and right sons (along with their subtrees).

For example, if Masha applies this operation to the root of the tree discussed above, it will take the following form:

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.

Input

The first line contains single integer tt (1≤t≤1041≤t≤104) — number of test cases.

In each test case, the first line contains an integer mm (1≤m≤2621441≤m≤262144), which is a power of two  — the size of the permutation pp.

The second line contains mm integers: p1,p2,…,pmp1,p2,…,pm (1≤pi≤m1≤pi≤m) — the permutation pp.

It is guaranteed that the sum of mm over all test cases does not exceed 3⋅1053⋅105.

Output

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.

Example

input

Copy

 

4

8

6 5 7 8 4 3 1 2

4

3 1 4 2

1

1

8

7 8 4 3 1 2 6 5

output

Copy

4
-1
0
-1

Note

Consider the first test.

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):

It can be shown that it is impossible to make a tree beautiful in fewer operations.

In the second test case, it can be shown that it is impossible to make a tree beautiful.

In the third test case, the tree is already beautiful.

一个从局部到整体的思想

先确定局部的先后顺序,维护树内的先后顺序

通过缩小树的范围确定每次是否要移动

每相邻两个点的编号的大小比较来确定是否要进行移动

#include<bits/stdc++.h>
using namespace std;
const int N=3e5;
int a[N];
int b[N];
int main()
{
	int t;
	cin>>t;
	int cnt=0;
	while(t--)
	{
		int n;
		cnt=0;
		cin>>n;
		for(int i=1;i<=n;i++)
		{
			cin>>a[i];
		}
		if(n==1)
		{
			cout<<0<<endl;
			continue;
		}
		int flag=0;
		while(1)
		{
			for(int i=1;i<=n;i+=2)
			{
				if(abs(a[i]-a[i+1])!=1)
				{
					flag=1;break;
				}
					if(a[i]==a[i+1]+1)
					{
						cnt++;
						swap(a[i],a[i+1]);			
					}
			}
			if(flag==1)
			break;
			int ans=0;
			for(int i=1;i<=n;i+=2)
			{
				ans++;
				b[ans]=a[i+1]/2;
			}
			n=ans;
			for(int i=1;i<=n;i++)
			{
				a[i]=b[i];
			}
			if(n==1)
			break;
		}
		if(flag==1)
		cout<<-1<<endl;
		else
		cout<<cnt<<endl;
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值