Permutation Transformation (Codeforces 1490D)
Tags: Implement, divide and conquer

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 \dots 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 $$$a_2=5$$$, and the left subtree will be the tree that will be built for the subarray $$$a[1 \dots 1] = [3]$$$, and the right one — for the subarray $$$a[3 \dots 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 $$$d_v$$$ the depth of the vertex $$$a_v$$$, that is, the number of edges on the path from the root to the vertex numbered $$$a_v$$$. Note that the root depth is zero. Given the permutation $$$a$$$, for each vertex, find the value of $$$d_v$$$.

Input: The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then $$$t$$$ test cases follow.The first line of each test case contains an integer $$$n$$$ ($$$1 \le n \le 100$$$) — the length of the permutation.This is followed by $$$n$$$ numbers $$$a_1, a_2, \ldots, a_n$$$ — permutation $$$a$$$.

Output: For each test case, output $$$n$$$ values — $$$d_1, d_2, \ldots, d_n$$$.


Sample Inputs:
3
5
3 5 2 1 4
1
1
4
4 3 1 2

Sample Outputs:
1 0 2 3 1 
0 
0 1 3 2

Solution (Python):
def permutacao(vetor, ind_e, ind_d, h):
    if ind_d - ind_e <= 0:
        return
    max_value = max(vetor[ind_e:ind_d])
    tree[max_value] = h
    permutacao(vetor, ind_e, vetor.index(max_value), h + 1)
    permutacao(vetor, vetor.index(max_value) + 1, ind_d, h + 1)
t = int(input())
for _ in range(t):
    tree = {}
    n = int(input())
    a = list(map(int, input().split()))
    permutacao(a, 0, n, 0)
    result = ' '.join((str(tree[e]) for e in a))
    print(result)

Solution (Python):
def solve(l, r, d=0):
    m = l
    mx = a[l]
    for i in range(l, r + 1):
        if a[i] > mx:
            mx = a[i]
            m = i
    ans[m] = d
    if a[l] < a[m]:
        solve(l, m - 1, d + 1)
    if a[r] < a[m]:
        solve(m + 1, r, d + 1)
    return
global ans
t = int(input())
for i in range(t):
    n = int(input())
    a = list(map(int, input().split()))
    ans = [0] * n
    solve(0, n - 1)
    print(*ans)

Solution (Python):
def build(l, r, a, d, cur_d=0):
    """"""
    if r < l:
        return
    if l == r:
        d[l] = cur_d
        return
    m = l
    for i in range(l + 1, r + 1):
        if a[m] < a[i]:
            m = i
    d[m] = cur_d
    build(l, m - 1, a, d, cur_d + 1)
    build(m + 1, r, a, d, cur_d + 1)

def solve():
    """"""
    n = int(input())
    a = list(map(int, input().split()))
    d = [0] * n
    build(0, n - 1, a, d)
    print(*d)

def main():
    """"""
    t = int(input())
    for _ in range(t):
        solve()
if __name__ == '__main__':
    main()