D. Masha and a Beautiful Tree

时间 2022/10/13 上午

D. Masha and a Beautiful Tree

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=2n.

题目url(https://codeforces.com/contest/1741/problem/D)

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.

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

Let’s enumerate m leaves of this tree from left to right. The leaf with the number i contains the value pi (1≤i≤m).

For example, if n=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 t (1≤t≤104) — number of test cases.

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

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

It is guaranteed that the sum of m over all test cases does not exceed 3⋅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.

输入
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
输出
4
-1
0
-1
基本思想
1.开两个数组进行交替操作
2.a为基本数组 b为简化后数组 
3.样例:
    8
    a: 6 5 7 8 4 3 1 2
step 1:
    a: 5 6 7 8 3 4 1 2
    cnt: +2
    a: 3 4 2 1
step 2:
    a:3 4 1 2
    cnt: +1
    a:2 1
step 3:
    a:1 2
    cnt: +1
    a:1
step 4:
    n==1(break)
    return cnt (=4)
代码模块
#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> pii;
typedef pair<long long,long long> pll;
typedef long long ll;
typedef unsigned long long ull;
#define N 262150 
#define M 200010

int T;
int n;
int a[N];
int b[N];

//判断a数组是否满足条件
//满足: 返回true 并且把cnt带回
//不满足:返回false
bool op_to(int a[],int n,int &cnt)
{
    for(int i=1;i<=n;i+=2)
    {
        if(abs(a[i]-a[i+1])!=1) return false;
        if(a[i]>a[i+1])
        {
            cnt++;
            swap(a[i],a[i+1]);
        }
    }
    return true;
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    cin>>T;
    while(T--)
    {
        for(int i=1;i<=N;i++) a[i] = 0;//初始化 以免出错
        cin>>n;
        for(int i=1;i<=n;i++) cin>>a[i];
        if(n==1)
        {
            cout<<0<<endl;
            continue;
        }

        bool flag = false;//标记数组
        int cnt = 0;//记录次数
        while(true)
        {
            if(n==1) break;//终止条件

            if(!op_to(a,n,cnt))
            {
                flag = true;
                break;
            }
            
            int ans = 0;
            for(int i=2;i<=n;i+=2) b[++ans] = a[i]/2;
            // for(int i=1;i<=ans;i++) cout<<b[i]<<" ";
            // cout<<endl;

            n = ans;
            for(int i=1;i<=n;i++) a[i] = b[i];//b为中间状态
        }

        if(flag) cout<<-1<<endl;
        else cout<<cnt<<endl;
    }
    system("pause");
    return 0;
}

方法暴力,凑合看

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值