105902024-04-06 10:37:50UVinceMágikus sorozatcpp17Accepted 100/10082ms41216 KiB
#include <bits/stdc++.h>
using namespace std;
using ll=long long;

#define all(a) a.begin(),a.end()

int main(){
    ios_base::sync_with_stdio(0);cin.tie(0);
    //freopen("be2.txt","r",stdin);
    int n;
    cin >> n;
    
    vector<ll> z(n),a(n);
    stack<pair<ll,ll>> last;
    vector<vector<pair<ll,ll>>> mark(n);
    for (ll i = 0; i < n; i++)
    {
        
        cin>>z[i];
        if (z[i]!=0) {
            a[i]=1;
            while (!last.empty() && last.top().first+last.top().second-1<=i) last.pop();
            last.push({i,z[i]});
            if (i+z[i]<n) mark[i+z[i]].push_back({i,z[i]});
        }
        else {
            a[i]=2;
            auto [f,l]=last.top();
            if (f+l-1>=i){
                a[i]=a[i-f];
            }
            else {
                auto [p,q]=last.top();
                last.pop();
                if (!mark[i].empty()){
                    vector<bool> allowed(1e4,true);
                    for (auto [x,y] : mark[i]){
                        allowed[a[y]]=false;
                    }
                    for (int z=2;z<1e4;z++){
                        if (allowed[z]){
                            a[i]=z;
                            break;
                        }
                    }
                }
            }
        }
    }
    for (ll i : a) cout<<i<<" ";
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms1696 KiB
2Accepted4ms2712 KiB
subtask215/15
3Accepted3ms2508 KiB
4Accepted3ms2760 KiB
5Accepted3ms2692 KiB
subtask315/15
6Accepted3ms2600 KiB
7Accepted2ms2540 KiB
8Accepted3ms2760 KiB
9Accepted2ms2812 KiB
10Accepted3ms2936 KiB
subtask430/30
11Accepted3ms3284 KiB
12Accepted3ms3288 KiB
13Accepted3ms3348 KiB
14Accepted3ms3264 KiB
subtask540/40
15Accepted59ms27080 KiB
16Accepted70ms30064 KiB
17Accepted79ms40152 KiB
18Accepted79ms41216 KiB
19Accepted82ms40572 KiB
20Accepted75ms40348 KiB
21Accepted72ms38116 KiB
22Accepted71ms38684 KiB
23Accepted76ms38212 KiB
24Accepted75ms34156 KiB
25Accepted72ms39876 KiB
26Accepted75ms39824 KiB
27Accepted71ms31412 KiB
28Accepted68ms30716 KiB
29Accepted64ms29220 KiB
30Accepted72ms37508 KiB
31Accepted68ms32812 KiB
32Accepted63ms29748 KiB