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;
typedef long long ll;
//~ #define ll int
#define int ll
typedef pair<int32_t, int32_t> pi;
typedef vector <int> vi;
typedef vector <pi> vpi;
typedef pair<pi, ll> pii;
typedef set <ll> si;
typedef long double ld;
#define f first
#define s second
#define mp make_pair
#define FOR(i,s,e) for(int i=s;i<=int(e);++i)
#define DEC(i,s,e) for(int i=s;i>=int(e);--i)
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define lbd(x, y) lower_bound(all(x), y)
#define ubd(x, y) upper_bound(all(x), y)
#define aFOR(i,x) for (auto i: x)
#define mem(x,i) memset(x,i,sizeof x)
#define fast ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define maxn 2000001
#define INF 1e9
#define MOD 1000000007
typedef pair <vi, int> pvi;
typedef pair <int,pi> ipi;
typedef vector <pii> vpii;
int N,D,T,A[maxn],L[maxn];
int pre[maxn]; // closest which covers it
bool spec[maxn];
vi adj[maxn];
int depth[maxn];
void dfs(int x){
aFOR(i,adj[x]){
depth[i] = depth[x] + 1; dfs(i);
}
}
int nxt[maxn]; bool del[maxn];
pi mxdepth[maxn];
int32_t main(){
fast;
cin >> N >> D >> T;
FOR(i,1,N) cin >> A[i];
stack <pi> sta;
// j covers i if A[j] + i - j <= T
// A[j] + j <= T - i
// increasing stack
FOR(i,1,N){
while (!sta.empty() && sta.top().f >= A[i] - i) sta.pop();
sta.push(pi(A[i] - i, i));
while (!sta.empty() && sta.top().f > T - i) sta.pop();
if (!sta.empty()) pre[i] = sta.top().s;
}
int mn = INF;
int ans = 0; //number which will rebel (covered)
FOR(i,1,N){
mn = min(mn, A[i] - i);
if (A[i] > T && mn <= T - i) spec[i] = 1;
if (mn <= T - i) ans++; // will rebel if no barriers
}
int r = 1;
FOR(i,1,N) if (spec[i]){
r = max(r, i+1);
while (r <= N && (!spec[r] || pre[r] > i)) r++;
nxt[i] = r;
if (r <= N){ // r is parent
adj[r].pb(i);
//~ cout << r << ' ' << i << '\n';
}
}
DEC(i,N,1) if (spec[i] && depth[i] == 0){
depth[i] = 1; dfs(i);
}
FOR(i,1,N){
mxdepth[i] = pi(depth[i], i);
aFOR(j,adj[i]) mxdepth[i] = max(mxdepth[i], mxdepth[j]);
}
set <pi> roots;
FOR(i,1,N) if (nxt[i] > N && spec[i]) roots.insert(pi(mxdepth[i].f, i));
FOR(i,1,D){
if (roots.empty()) break;
int optRoot = roots.rbegin()->s; roots.erase(--roots.end());
int opt = mxdepth[optRoot].s;
ans -= mxdepth[optRoot].f;
for (int x = opt; x <= N && !del[x]; x = nxt[x]){
del[x] = 1;
aFOR(j, adj[x]) if (!del[j]) roots.insert(pi(mxdepth[j].f, j));
}
}
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... |