【題解】Codeforces 1490D. Permutation Transformation

【題目敘述】https://codeforces.com/contest/1490/problem/D

#include <bits/stdc++.h>
using namespace std;
 
int t, n, a[105], d[105];
 
void f(int l, int r, int dep){
    if (l > r) return;
    int mx = 0, idx;
    for (int i = l; i <= r; i++){
        if (a[i] > mx){
            mx = a[i];
            idx = i;
        }
    }
    d[idx] = dep;
    f(l, idx-1, dep+1);
    f(idx+1, r, dep+1);
}
 
int main(){
    cin >> t;
    while (t--){
        cin >> n;
        for (int i = 1; i <= n; i++){
            cin >> a[i];
        }
        f(1, n, 0);
        for (int i = 1; i <= n; i++){
            cout << d[i] << " ";
        }
        cout << "\n";
    }
}
分享本文 Share with friends