D - Permutation Transformation(递归)

D - Permutation Transformation(递归)

题干

A permutation — is a sequence of length n integers from 1 to n, in which all the numbers occur exactly once. For example, [1], [3,5,2,1,4], [1,3,2] — permutations, and [2,3,2], [4,3,1], [0] — no.
Polycarp was recently gifted a permutation a[1…n] of length n. Polycarp likes trees more than permutations, so he wants to transform permutation a into a rooted binary tree. He transforms an array of different integers into a tree as follows:
the maximum element of the array becomes the root of the tree;
all elements to the left of the maximum — form a left subtree (which is built according to the same rules but applied to the left part of the array), but if there are no elements to the left of the maximum, then the root has no left child;
all elements to the right of the maximum — form a right subtree (which is built according to the same rules but applied to the right side of the array), but if there are no elements to the right of the maximum, then the root has no right child.
For example, if he builds a tree by permutation a=[3,5,2,1,4], then the root will be the element a2=5, and the left subtree will be the tree that will be built for the subarray a[1…1]=[3], and the right one — for the subarray a[3…5]=[2,1,4].
As a result, the following tree will be built:The tree corresponding to the permutation a=[3,5,2,1,4].
Another example: let the permutation be a=[1,3,2,7,5,6,4]. In this case, the tree looks like this:The tree corresponding to the permutation a=[1,3,2,7,5,6,4].
Let us denote by dv the depth of the vertex av, that is, the number of edges on the path from the root to the vertex numbered av. Note that the root depth is zero. Given the permutation a, for each vertex, find the value of dv.

Input
The first line contains one integer t (1≤t≤100) — the number of test cases. Then t test cases follow.
The first line of each test case contains an integer n (1≤n≤100) — the length of the permutation.
This is followed by n numbers a1,a2,…,an — permutation a.

Output
For each test case, output n values — d1,d2,…,dn.

Example

Input
3
5
3 5 2 1 4
1
1
4
4 3 1 2

Output
1 0 2 3 1
0
0 1 3 2

题解

先大致翻译一下题干:
给定n元素数组a,将数组a转化为一棵二叉树,规则如下:
1.a中最大值作二叉树之根
2.最大值左侧的子数组构成左子树
3.最大值右侧的子数组构成右子树
4.子树的构成规则也如上
任务是顺序输出数组中每个数在树中的深度
数据量很小的一道题,很容易想到递归的写法,将数组和深度数组设为全局变量,传入每段树的左右界递归即可。

#include <bits/stdc++.h>
#include <bits/extc++.h>

using namespace std;
typedef long long ll;

int tree[105];
int res[105];

void recur(int left,int right,int depth){
    if(left > right){
        return;
    }
    int maxNumIndex = left;
    for(int i  = left ; i <= right ; ++i){
        if(tree[i] > tree[maxNumIndex]){
            maxNumIndex = i;
        }
    }
    res[maxNumIndex] = depth;
    recur(left,maxNumIndex - 1,depth + 1);
    recur(maxNumIndex + 1,right,depth + 1);
    return;
}

int main()
{
    int count;
    cin>>count;
    while(count--){
       int n;
       cin>>n;
       for(int i = 0 ; i < n ; ++i){
            scanf("%d",tree+i);
       }
       memset(res,0,sizeof(res));
       recur(0,n-1,0);
       for(int i = 0 ; i < n ; ++i){
            cout<<res[i]<<' ';
       }
       cout<<endl;
    }
    return 0;
}
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值