Source Code
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 9;
const int mod = 1e9 + 7;

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    int t;
    cin >> t;
    while(t--){
        int n;
        cin >> n;
        int a[n];
        pair<int,int> b[n];
        for(int i = 0; i < n; i++){
            cin >> a[i];
            b[i].first = i + 1 - a[i];
            b[i].second = i;
        }
        sort(b, b + n);
        int used[n] = {0};
        int res[n] = {0};
        for(int i = 0 ; i < n; i++){
            int ind = n;
            int l = 0 , r = n - 1;
            while(l <= r){
                int m = l + (r - l) /2;
                if(b[m].first <= a[i] + i + 1){
                    ind = m;
                    l = m + 1;
                }else r = m - 1;
            }
            if(ind != n){
                for(int j = ind; j >= 0; j--){
                    if(used[j]) break;
                    used[j] = 1;
                    res[b[j].second] = i + 1;
                }
            }
        }
        for(int i = 0; i < n; i++){
            if(res[i] == 0) cout << -1 << " ";
            else cout << res[i] << " ";
        }
        cout << "\n";
    }
    return 0;
}  
Copy
Easy challenge? AbduSaber
GNU G++17
141 ms
5.4 MB
Accepted