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>
#define ll long long
#define ff first
#define ss second
#define pb push_back
using namespace std;
const int N = 2e6+50, mod = 998244353, K = 31;
int n,d,T;
int cnt = 0;
int L[N],R[N],B[N],A[N],gar[N],shig[N];
bool fix[N];
pair<int,int>mx[N*4];
int toadd[N*4];
inline void upd(int node,int tl,int tr,int l,int r,int val){
if(tl > r || l > tr) return ;
if(tl == tr){
mx[node].ff += val;
mx[node].ss = tl;
return ;
}
if(l <= tl && tr <= r){
mx[node].ff += val;
toadd[node] += val;
return ;
}
int mid = (tl+tr)/2;
int left = node*2;
int right = left+1;
if(toadd[node]){
toadd[left] += toadd[node];
toadd[right] += toadd[node];
mx[left].ff += toadd[node];
mx[right].ff += toadd[node];
toadd[node] = 0;
}
upd(left,tl,mid,l,r,val);
upd(right,mid+1,tr,l,r,val);
mx[node] = max(mx[left],mx[right]);
}
inline void upd1(int node,int tl,int tr,int l,int r){
if(tl > r || l > tr) return ;
if(tl == tr){
mx[node].ff = L[tl+1] - L[tl];
mx[node].ss = tl;
return ;
}
int mid = (tl+tr)/2;
int left = node*2;
int right = left+1;
if(toadd[node]){
toadd[left] += toadd[node];
toadd[right] += toadd[node];
mx[left].ff += toadd[node];
mx[right].ff += toadd[node];
toadd[node] = 0;
}
upd1(left,tl,mid,l,r);
upd1(right,mid+1,tr,l,r);
mx[node] = max(mx[left],mx[right]);
}
inline void test_case(){
cin >> n >> d >> T;
for(int i = 1; i <= n; i++){
cin >> A[i];
if(A[i] <= T){
cnt++;
L[cnt] = i;
R[cnt] = min(n,T-A[i]+i);
B[i]++;
B[T-A[i]+i+1]--;
}
}
int answer = n;
for(int i = 1; i <= n; i++){
B[i] += B[i-1];
if(B[i] == 0){
answer--;
}
}
gar[1] = 1;
for(int i = 2; i <= cnt; i++){
if(R[i] < R[i-1]) gar[i] = gar[i-1]; else gar[i] = i;
}
shig[n] = n;
for(int i = n-1; i >= 1; i--){
if(R[i] > R[i+1]) shig[i] = shig[i+1]; else shig[i] = i;
}
if(cnt <= d){
cout << cnt << endl;
return ;
}
for(int i = 1; i <= cnt; i++){
int daf = R[i] - L[i];
if(i < cnt) daf -= (R[i+1]-L[i+1]);
upd(1,1,cnt,i,i,daf);
}
while(d--){
pair<int,int> p = mx[1];
int val = p.ff-1;
int ind = p.ss;
answer -= val;
//cout << val << endl;
if(fix[ind] == 1){
upd1(1,1,cnt,ind,0);
} else {
upd1(1,1,cnt,ind,0);
int val = R[ind]-R[shig[ind]];
if(val)upd(1,1,cnt,ind,shig[ind],-val);
if(gar[ind] < ind)upd1(1,1,cnt,gar[ind],ind-1);
}
}
cout << answer << endl;
}
main(){
ios::sync_with_stdio(0);
int T = 1;
//cin >> T;
for(int i = 1; i <= T; i++){
test_case();
}
}
Compilation message (stderr)
prison.cpp:137:2: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
137 | main(){
| ^~~~
# | 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... |