This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MX=2e6+5;
int N,D,T;
int A[MX],B[MX];
bool C[MX];
int main() {
        cin.tie(0); ios_base::sync_with_stdio(0);
        cin>>N>>D>>T;
        for(int i=1;i<=N;i++) {
                cin>>A[i];
                B[i]=A[i]-i;
        }
        for(int d=0;d<D;d++) {
                vector<int> stk;
                vector<int> cnt(N+1);
                for(int i=1;i<=N;i++) {
                        while(stk.size() && B[stk.back()]>=B[i]) stk.pop_back();
                        if(A[i]<=T) {
                                stk.push_back(i);
                        } else {
                                int l=0,r=stk.size()-1,p=-1;
                                while(l<=r) {
                                        int m=(l+r)/2;
                                        if(A[stk[m]]+i-stk[m]<=T) {
                                                l=m+1,p=m;
                                        } else {
                                                r=m-1;
                                        }
                                }
                                if(p!=-1) {
                                        cnt[stk[p]]+=1;
                                        cnt[i]-=1;
                                }
                        }
                        if(C[i]) stk.clear();
                }
                int arg=0;
                for(int i=1;i<=N;i++) {
                        cnt[i]+=cnt[i-1];
                        if(cnt[i]>cnt[arg] && A[i]<=T) arg=i;
                }
                C[arg]=true;
        }
        vector<int> stk;
        int ans=0;
        for(int i=1;i<=N;i++) {
                while(stk.size() && B[stk.back()]>=B[i]) stk.pop_back();
                if(A[i]<=T) {
                        ans++;
                        stk.push_back(i);
                } else {
                        if(stk.size() && A[stk.front()]+i-stk.front()<=T) ans++;
                }
                if(C[i]) stk.clear();
        }
        cout<<ans<<'\n';
}
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |