Submission #575823

#TimeUsernameProblemLanguageResultExecution timeMemory
575823jiahngThe short shank; Redemption (BOI21_prison)C++14
100 / 100
432 ms221856 KiB
#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
	}
	//~ cout << ans << '\n';
	mn = INF;
	stack <pi> sta;
	FOR(i,1,N) nxt[i] = N+1;
	FOR(i,1,N){
		while (!spec[i] && i <= N) i++;
		if (i > N) break;
		
		int mn = A[i] - i;
		for (int j = i+1; j <= N && !spec[j]; j++) mn = min(mn, A[j] - j);
		

		while (!sta.empty() && sta.top().f > T - i){
			pi x = sta.top(); sta.pop();
			nxt[x.s] = i; adj[i].pb(x.s);
			//~ cout << i << ' ' << x.s << '\n';
		}
		sta.push(pi(mn, i));
		mn = INF;
	}

	DEC(i,N,1) if (spec[i] && depth[i] == 0){
		depth[i] = 1; dfs(i);
	}
	
	FOR(i,1,N) if (spec[i]){
		mxdepth[i] = pi(1, i);
		aFOR(j,adj[i]) mxdepth[i] = max(mxdepth[i], pi(mxdepth[j].f+1, mxdepth[j].s));
	}
	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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...