#include<bits/stdc++.h>
using namespace std;
const int maxn=2000000+10;
long long n,k,t,inf=1e9+5;
int all[maxn],res=0,vas[maxn],kaf=(1<<21);
vector<pair<int,int>>allq;
set<int>alld[maxn];
struct segment{
struct node{
long long lazy;
pair<long long,long long>mx;
node(){
lazy=0;
mx=make_pair(0,0);
}
};
node seg[(1<<22)];
segment(){
for(int i=(1<<22)-1;i>=1;i--){
if(i>=kaf){
seg[i].mx.second=i-kaf;
}else{
seg[i].mx.second=seg[(i<<1)].mx.second;
}
}
}
void calc(int i){
if(i>=kaf){
seg[i].mx.first+=seg[i].lazy;
seg[i].lazy=0;
return ;
}
seg[i].mx=max(make_pair(seg[(i<<1)].lazy+seg[(i<<1)].mx.first,seg[(i<<1)^1].mx.second),make_pair(seg[(i<<1)^1].lazy+seg[(i<<1)^1].mx.first,seg[(i<<1)^1].mx.second));
}
void shift(int i){
if(i>=kaf){
return ;
}
seg[(i<<1)].lazy+=seg[i].lazy;
seg[(i<<1)^1].lazy+=seg[i].lazy;
seg[i].lazy=0;
return ;
}
void upd(int i,int l,int r,int tl,int tr,int w){
if(l>r||l>tr||r<tl||tl>tr){
return ;
}
if(l>=tl&&r<=tr){
seg[i].lazy+=w;
shift(i);
calc(i);
return ;
}
shift(i);
int m=(l+r)>>1;
upd((i<<1),l,m,tl,tr,w);
upd((i<<1)^1,m+1,r,tl,tr,w);
calc(i);
}
pair<int,int> pors(int i,int l,int r,int tl,int tr){
if(l>r||l>tr||r<tl||tl>tr){
return make_pair(0,-1);
}
shift(i);
calc(i);
if(l>=tl&&r<=tr){
return seg[i].mx;
}
int m=(l+r)>>1;
return max(pors((i<<1),l,m,tl,tr),pors((i<<1)^1,m+1,r,tl,tr));
}
}segmx;
void remove(int ind){
if(vas[ind]==1){
return ;
}
vas[ind]=1;
segmx.upd(1,0,kaf-1,allq[ind].first,allq[ind].second,-1);
}
struct segmentv{
vector<int>seg[(1<<22)];
void upd(int i,int l,int r,int tl,int tr,int w){
if(l>r||l>tr||r<tl||tl>tr){
return ;
}
if(l>=tl&&r<=tr){
seg[i].push_back(w);
return ;
}
int m=(l+r)>>1;
upd((i<<1),l,m,tl,tr,w);
upd((i<<1)^1,m+1,r,tl,tr,w);
return ;
}
void del(int i){
i+=kaf;
while(i>0){
for(auto x:seg[i]){
remove(x);
}
seg[i].clear();
i>>=1;
}
}
}segv;
void vorod(){
cin>>n>>k>>t;
for(int i=1;i<=n;i++){
cin>>all[i];
}
}
void calbaz(){
all[0]=inf;
vector<int>v;
v.push_back(0);
for(int i=1;i<=n;i++){
while((int)v.size()>0&&all[v.back()]+(i-v.back())>=all[i]){
v.pop_back();
}
while((int)v.size()>0&&all[v.back()]+(i-v.back())>t){
v.pop_back();
}
if(all[i]>t){
if((int)v.size()==0){
res++;
}else{
allq.push_back(make_pair(v.back(),i-1));
}
}
v.push_back(i);
}
}
void aval(){
for(int i=0;i<allq.size();i++){
segmx.upd(1,0,kaf-1,allq[i].first,allq[i].second,1);
segv.upd(1,0,kaf-1,allq[i].first,allq[i].second,i);
// for(int j=allq[i].first;j<=allq[i].second;j++){
// alld[j].insert(i);
// }
}
}
void pre(){
calbaz();
aval();
}
void cal(){
// int ind=getmx();
pair<int,int>gm=segmx.pors(1,0,kaf-1,0,n+1);
if(gm.first==0){
return ;
}
res+=gm.first;
segv.del(gm.second);
}
void solve(){
for(int i=0;i<k;i++){
cal();
}
}
void khor(){
res=n-res;
cout<<res<<"\n";
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
//freopen("inp.txt","r",stdin);
vorod();
pre();
solve();
khor();
}
Compilation message
prison.cpp: In function 'void aval()':
prison.cpp:140:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
140 | for(int i=0;i<allq.size();i++){
| ~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
153 ms |
291924 KB |
Output is correct |
2 |
Incorrect |
115 ms |
291920 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
116 ms |
292100 KB |
Output is correct |
2 |
Correct |
274 ms |
306460 KB |
Output is correct |
3 |
Correct |
235 ms |
303216 KB |
Output is correct |
4 |
Correct |
280 ms |
308284 KB |
Output is correct |
5 |
Correct |
323 ms |
312784 KB |
Output is correct |
6 |
Correct |
305 ms |
309200 KB |
Output is correct |
7 |
Correct |
691 ms |
354756 KB |
Output is correct |
8 |
Correct |
287 ms |
312012 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
153 ms |
291924 KB |
Output is correct |
2 |
Incorrect |
115 ms |
291920 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
109 ms |
291860 KB |
Output is correct |
2 |
Incorrect |
145 ms |
294612 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
153 ms |
291924 KB |
Output is correct |
2 |
Incorrect |
115 ms |
291920 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
153 ms |
291924 KB |
Output is correct |
2 |
Incorrect |
115 ms |
291920 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |