Source Code
#include<bits/stdc++.h>
///#include "cmake-build-debug/Car.h"
using namespace std;
#define endl         '\n'
const int N = 2e5 + 5 ;
void fast() {
    std::ios_base::sync_with_stdio(0);
    cin.tie(NULL);
#ifndef ONLINE_JUDGE
    freopen("Input.txt", "r", stdin);
    freopen("Output.txt", "w", stdout);
#endif
}
int n, t  ;
vector<int>a(N) ;
int main() {
    fast() ;
    cin>>t ;
    while (t--)
    {
        cin>>n ;
        for (int i = 1 ; i <= n ; i++)
            cin>>a[i] ;

        int mx = INT_MIN ;
        vector<pair<int,int>>mx_pair ;
        for (int j=1 ;j<=n;j++)
        {
            int cur = a[j] + j ;
            if (cur > mx )
            {
                mx = cur ;
                mx_pair.emplace_back(mx , j ) ;
            }
        }
        for (int i=1 ; i <= n ; i ++)
        {
            int cur = i - a[i] ;
            pair<int,int> p = {cur , 0 } ;
            int idx = lower_bound(mx_pair.begin() , mx_pair.end() , p ) - mx_pair.begin() ;
            if (idx == (int)(mx_pair.size()))
            {
                cout << -1 << ' ' ;
            }
            else
            {
                cout << mx_pair[idx].second << ' ' ;
            }
        }
        cout << endl ;
    }
    return  0 ;
}
Copy
Easy challenge? Zidaan
GNU G++17
76 ms
4.1 MB
Accepted