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;
const int INF=INT_MAX/2;
int main(){
cin.tie(nullptr)->sync_with_stdio(false);
int n,d,t;
cin >> n >> d >> t;
vector<int> a(n);
for(auto &x:a)cin >> x;
vector<int> pre(n,-1);
stack<pair<int,int>> s;
s.emplace(-INF,-1);
int ans=0;
for(int i=0;i<n;i++){
int x=min(a[i],t)-i;
while(s.top().first>x)s.pop();
if(a[i]>t)pre[i]=s.top().second;
else ans++;
s.emplace(a[i]-i,i);
}
vector<pair<int,int>> range;
for(int i=0;i<n;i++)if(pre[i]!=-1)range.emplace_back(pre[i],i-1);
int m=range.size();
ans+=m;
vector<vector<int>> g(m);
vector<int> par(m,-1);
stack<pair<int,int>> st;
for(int i=0;i<m;i++){
int x=range[i].first;
while(!st.empty()&&st.top().first>=x){
int j=st.top().second;
st.pop();
g[i].emplace_back(j);
par[j]=i;
}
st.emplace(x,i);
}
vector<int> b,dep(m),cnt(m+1);
function<int(int)> dfs=[&](int u){
int deep=-1;
for(auto v:g[u])if(dfs(v)>dep[u])dep[u]=dep[v],deep=v;
for(auto v:g[u])if(v!=deep)cnt[dep[v]]++;
return ++dep[u];
};
for(int i=0;i<m;i++)if(par[i]==-1)cnt[dfs(i)]++;
for(int i=m;i>=0;i--)while(cnt[i]--&&d-->0)ans-=i;
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... |