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;
#define MAX_N 2000006
#define INF 0x3f3f3f3f
int sz;
pair <int ,int> tree[2*MAX_N];
void bld(vector<int>&arr){
for(int i=sz; i<(sz<<1); i++)
tree[i] = make_pair(arr[i-sz] ,i-sz);
for(int i=sz-1; i; i--)
tree[i] = min(tree[i<<1] ,tree[i<<1|1]);
}
void upd(int p ,int v){
for(tree[p+=sz].first = v; p>1; p>>=1)
tree[p>>1] = min(tree[p] ,tree[p^1]);
}
pair <int ,int> qry(int l ,int r){
auto res = make_pair(INF ,INF);
for(l+=sz ,r+=sz+1; l<r; l>>=1 ,r>>=1){
if(l&1) res = min(res ,tree[l++]);
if(r&1) res = min(res ,tree[--r]);
}
return res;
}
array <int ,3> tree2[2*MAX_N];
array <int ,3> mrg(array <int ,3> lf ,array <int ,3> rt){
if(lf[2] > rt[2]) swap(lf ,rt);
array <int ,3> nw = lf;
nw[0] += rt[0];
if(lf[0] + rt[1] > nw[1]){
nw[1] = lf[0] + rt[1];
nw[2] = rt[2];
}
return nw;
}
void bld2(vector<int>&arr){
for(int i=sz; i<(sz<<1); i++)
tree2[i] = {arr[i-sz] ,arr[i-sz] ,i-sz};
for(int i=sz-1; i; i--)
tree2[i] = mrg(tree2[i<<1] ,tree2[i<<1|1]);
}
void upd2(int p ,int v){
p += sz;
tree2[p][0] += v;
tree2[p][1] += v;
for(; p>1; p>>=1)
tree2[p>>1] = mrg(tree2[p] ,tree2[p^1]);
}
int main()
{
int n ,k ,T;
scanf("%d%d%d",&n,&k,&T) ,sz = n+1;
vector <int> a(n);
for(int&i : a)
scanf("%d",&i);
int ans = 0;
vector <int> lf(n+1 ,INF);
vector <pair<int ,int>> q;
for(int i = 0; i < n; i++){
while(!q.empty() && q.back().first > T-i)
q.pop_back();
if(a[i] <= T){
while(!q.empty() && q.back().first >= a[i]-i)
q.pop_back();
q.push_back({a[i]-i ,i});
}else if(!q.empty())
lf[i-1] = q.back().second;
else
ans++;
}
vector <int> cum(n+1);
for(int i = 0; i < n; i++) if(lf[i] != INF)
cum[lf[i]]++ ,cum[i+1]--;
bld(lf);
bld2(cum);
for(int s = 0; s < k && tree2[1][1]; s++){
int rem = tree2[1][2];
while(true){
auto q = qry(rem ,n-1);
if(q.first > rem)
break;
upd2(lf[q.second] ,-1);
upd2(q.second+1 ,+1);
lf[q.second] = INF;
upd(q.second ,INF);
ans++;
}
}
printf("%d\n",n-ans);
}
Compilation message (stderr)
prison.cpp: In function 'int main()':
prison.cpp:56:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
56 | scanf("%d%d%d",&n,&k,&T) ,sz = n+1;
| ~~~~~^~~~~~~~~~~~~~~~~~~
prison.cpp:59:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
59 | scanf("%d",&i);
| ~~~~~^~~~~~~~~
# | 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... |