#include <bits/stdc++.h>
using namespace std;
using ll = int;
using vll = vector<ll>;
using vvl = vector<vll>;
using pll = pair<ll,ll>;
using vpl = vector<pll>;
using vvp = vector<vpl>;
#define f first
#define s second
#define pb push_back
#define all(v) v.begin(),v.end()
vvl g;
vll vis, dep, bes, pr;
void dfs(ll cur){
bes[cur]=cur;
for(ll i:g[cur]){
dep[i]=dep[cur]+1;
pr[i]=cur;
dfs(i);
if(dep[bes[i]]>dep[bes[cur]])bes[cur]=bes[i];
}
}
ll m = 0;
void ear(priority_queue<pll>& pq, ll i){
if(vis[i]==1)return;
m++;
vis[i]=1;
for(ll j:g[i]){
if(!vis[j]){
pq.push({dep[bes[j]]-dep[j],j});
}
}
if(pr[i]!=-1)ear(pq, pr[i]);
}
int main(){
ios_base::sync_with_stdio(0);cin.tie(NULL);
ll n, d, t;
cin >> n >> d >> t;
vll tm(n), ls(n);
stack<pll> w;
w.push({-1e9, -1});
for(ll i = 0; i < n; ++i){
cin >> tm[i];
w.push({tm[i], i});
while(w.top().f+i-w.top().s>t){
w.pop();
}
ls[i]=w.top().s;
if(ls[i]==-1)m++;
}
pr = vll(n,-1);
stack<pll> q;
g = vvl(n);
vis=dep=bes=vll(n);
vll lon;
priority_queue<pll> pq;
for(ll i = n-1; i >= 0; --i){
if(ls[i]==i || ls[i]==-1)continue;
while(!q.empty()){
if(q.top().f>=i)q.pop();
else break;
}
if(q.empty()){lon.pb(i);q.push({ls[i], i});continue;}
g[q.top().s].pb(i);
q.push({ls[i], i});
}
for(ll i: lon){
dfs(i);
pq.push({dep[bes[i]], i});
}
while(d-- && !pq.empty()){
auto [a,b] = pq.top();
pq.pop();
ear(pq, bes[b]);
}
cout << n-m;
}
# | 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... |