이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e6+1;
int a[MAXN],lf[MAXN],par[MAXN],sub[MAXN],nxt[MAXN];
bool ded[MAXN];
vector<int>adj[MAXN];
#define owo ios_base::sync_with_stdio(0);cin.tie(0);
void dfs(int v){
int mx = 0;
for(int x:adj[v]){
dfs(x);
if(mx < sub[x]){
mx = sub[x];
nxt[v] = x;
}
}
sub[v] = mx+1;
}
int main()
{
owo
int n,d,t;
cin>>n>>d>>t;
for(int i=0;i<n;i++){
cin>>a[i];
lf[i] = -1;
}
int ans = 0;
stack<int>stk;
for(int i=0;i<n;i++){
if(a[i]<=t){
ans++;
while(!stk.empty() && a[stk.top()] >= a[i])stk.pop();
stk.push(i);
}else{
while(!stk.empty() && i-stk.top() > t-a[stk.top()])stk.pop();
if(stk.empty())continue;
ans++;
lf[i] = stk.top();
}
}
while(!stk.empty())stk.pop();
for(int i=n-1;i>=0;i--){
if(lf[i] == -1)continue;
while(!stk.empty() && lf[stk.top()] >= i)stk.pop();
if(!stk.empty()){
par[i] = stk.top();
adj[par[i]].push_back(i);
}
else par[i] = -1;
while(!stk.empty() && lf[i] <= lf[stk.top()])stk.pop();
stk.push(i);
//cout<<i<<" "<<par[i]<<'\n';
}
for(int i=0;i<n;i++)nxt[i] = -1;
for(int i=0;i<=n-1;i++){
if(par[i] == -1)dfs(i);
}
vector<vector<int>> s(n+1);
//for(int i=0;i<n;i++)cout<<lf[i]<<" ";
//cout<<'\n';
for(int i=0;i<n;i++){
//cout<<sub[i]<<" ";
s[sub[i]].push_back(i);
}
//cout<<'\n';
for(int i=n;i>=1;i--){
for(int x:s[i]){
if(ded[x])continue;
ans-=i;
int v = x;
while(v!=-1){
ded[v] = 1;
v = nxt[v];
}
d--;
if(!d)break;
}
if(!d)break;
}
cout<<ans<<'\n';
}
# | 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... |