분할시키면서 실제 subtree도 분할시켰습니다.
import sys
input = sys.stdin.readline
def make_tree(depth, a):
if not a:
return a
max_num = 0
for idx, num in enumerate(a):
if max_num < num:
max_idx = idx
max_num = num
answer[dic[max_num]] = depth
make_tree(depth+1, a[:max_idx])
make_tree(depth+1, a[max_idx+1:])
for test in range(int(input())):
n = int(input())
arr = list(map(int, input().split()))
dic = {num: idx for idx, num in enumerate(arr)}
answer = [0] * n
make_tree(0, arr)
print(*answer)
'practivceAlgorithm > codeforce' 카테고리의 다른 글
[codeforce][Python] #690 A. Favorite Sequence (0) | 2021.12.07 |
---|---|
[Codeforce][Python] #702 F. Equalize the Array (0) | 2021.11.07 |
[Codeforce][Python] #702 E. Accidental Victory (0) | 2021.11.07 |
[Codeforce][Python] #702 C. Sum of Cubes (0) | 2021.11.07 |
[Codeforce][Python] #702 B. Balanced Remainders (0) | 2021.11.07 |