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
    评论
玛莎和熊高清壁纸和浏览器的新标签主题 我们向您展示了我们的Masha和Bear扩展程序,它们将使您的浏览器拥有全新的外观,并使之更加有趣和丰富多彩。 忘了您无聊的新标签页,并通过安装此应用让它变得更加令人兴奋。 这些高质量的主题将在您浏览时显示,并使您的整体体验更好。 此外,我们还包含一些其他有用的功能,以使您更加享受它。 随时与您的朋友分享此应用。 如何使用:-通过单击添加到Chrome按钮安装此扩展程序,它将自动添加。 加载扩展程序时,请等待几秒钟,然后执行最佳操作! -在左上角,“齿轮”。 这将加载设置面板,您可以在其中使用切换按钮来打开或关闭功能。 此扩展程序提供一些选项,让您仅具有高清背景(选择“收藏夹”或使其随机化)或使用一些出色的功能。 特点-居中,时钟(12h / 24h选项),带有个性化问候语供您选择(例如昵称)-出色的工作清单,以便您可以跟踪重要内容-快捷网站的快捷方式(单击左上角的“链接”图标) 。 -每日行情–每天都会为您和您的动机选择有力的报价来激发灵感。 -单击左上角的齿轮图标-选择仅包含墙纸,或选择所需的功能。 -在同一面板中,单击照片,然后选择您喜欢的照片,或单击齿轮旁边的“刷新”按钮以选择下一张壁纸。 有了这个扩展程序,您将获得墙纸,艺术品,玛莎和熊的素描以及更多其他作品。 卸载要从浏览器中删除扩展名,请执行以下步骤:-在屏幕右上角找到三个垂直点图标-转到“设置”。 -在出现的菜单中,选择“扩展名”。 -从已安装的扩展程序列表中,找到要卸载的扩展程序的名称。 -单击“启用”右侧的垃圾桶图标(对于您要卸载的扩展名)。 -我们的扩展程序应从您的Chrome浏览器中删除。 更简单的方法–右键单击工具栏上的图标,然后单击“从Chrome中删除...”。免责声明:此扩展名由Masha和Bear粉丝们为粉丝们制作。 我们和内容与任何公司都不隶属,赞助或批准,并且是非官方的。 如果您认为我们侵犯了您的版权,请与我们联系。 尽管此扩展程序是免费的,但某些部分可以包含广告。 支持语言:English

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值