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 all(x) (x).begin(), (x).end()
#define sz(x) ((int) x.size())
#define show(x) cerr << #x << " is " << x << endl;
#define show2(x, y) cerr << #x << " is " << x << ", " << #y << " is " << y << endl;
#define show3(x, y, z) cerr << #x << " is " << x << ", " << #y << " is " << y << ", " << #z << " is " << z << endl;
#define l first
#define r second
typedef long long lint;
typedef pair<lint, lint> ii;
lint arr[2000005];
int p[2000005];
vector<int> child[2000005];
int vis[2000005];
ii maxh[2000005];
int cnt = 0;
void dfs(int u){
maxh[u] = ii(0, u);
for(int v : child[u]){
dfs(v);
maxh[u] = max(maxh[u], maxh[v]);
}
maxh[u].first += 1;
}
priority_queue<ii> pq;
int main(){
ios_base::sync_with_stdio(false); cin.tie(0);
lint n, S, T; cin >> n >> S >> T;
for(int i = 1;i <= n;i++) cin >> arr[i];
lint ans = n;
vector<ii> useful;
vector<ii> ranges;
for(int i = 1;i <= n;i++){
if(arr[i] <= T){
int reach = min(n, i+T-arr[i]);
//show2(i, reach);
while(not useful.empty()){
if(useful.back().first <= reach) useful.pop_back();
else break;
}
useful.push_back(ii(reach,i));
}
else{
auto it = lower_bound(all(useful), ii(i, -1), greater<ii>());
if(it != useful.begin()){
it--;
ranges.push_back(ii(it->second,i));
}
else{
ans--;
}
}
}
sort(all(ranges), [&](ii a, ii b){
if(a.l == b.l) return a.r > b.r;
else return a.l < b.l;
});
stack<ii> ST;
for(ii R : ranges){
while(not ST.empty()){
if(ST.top().first <= R.first) ST.pop();
else break;
}
cnt++;
if(not ST.empty()) p[cnt] = ST.top().second;
ST.push(ii(R.r, cnt));
}
for(int i = 1;i <= cnt;i++) child[p[i]].push_back(i);
dfs(0);
pq.push(maxh[0]);
//for(int i = 0;i <= cnt;i++){
// show2(maxh[i].first, maxh[i].second);
//}
while(S--){
if(pq.empty()) break;
ii t = pq.top(); pq.pop();
ans -= t.first;
int u = t.second;
//show2(t.first, u);
while(not vis[u]){
int par = p[u];
//show2(u,par);
vis[u] = true;
if(vis[par]) break;
for(int v : child[par]){
if(v != u){
pq.push(maxh[v]);
//show3(u,par,v);
}
}
u = par;
}
}
///potenially ans++ here cause of 0
ans++;
cout << ans;
}
# | 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... |