Submission #1278267

#TimeUsernameProblemLanguageResultExecution timeMemory
1278267vlomaczkThe short shank; Redemption (BOI21_prison)C++20
80 / 100
202 ms129408 KiB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
typedef long long ll;
typedef long double ld;
using namespace __gnu_pbds;
using namespace std;

template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

int M = 1e6 + 10;
vector<vector<int>> g(M);
vector<pair<int, int>> h(M);

void dfs(int v) {
	h[v] = {1,-1};
	for(auto u : g[v]) {
		dfs(u);
		h[v] = max(h[v], {h[u].first+1, u});
	}
}

int best = 0;
struct my_pq {
	vector<vector<pair<int, int>>> elements;
	int cnt = M-1;

	my_pq() : elements(M, vector<pair<int, int>>()) {}

	void pop() {
		top(); elements[cnt].pop_back();
	}

	pair<int, int> top() {
		while(elements[cnt].empty()) cnt--;
		return elements[cnt].back();
	}

	void push(pair<int, int> p) {
		elements[p.first].push_back(p);
	}
};
my_pq pq;

void add_roots(int v) {
	if(h[v].second==-1) return;
	for(auto u : g[v]) {
		if(h[v].second==u) add_roots(u);
		else pq.push({h[u].first, u});
	}
}

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);

	ll n, d, T;
	cin >> n >> d >> T;
	vector<ll> t(n);
	for(int i=0; i<n; ++i) cin >> t[i];
	ll inf = 1e18;
	ld start = 0;
	ld koniec = 1e18;
	vector<ll> left(n, -1);
	int ans = 0;
	stack<int> stos;
	for(int i=0; i<n; ++i) {
		if(t[i] <= T) {
			left[i]=i;
			stos.push(i);
			continue;
		}
		while(stos.size() && stos.top() - t[stos.top()] < i-T) stos.pop();
		if(stos.size()) left[i] = stos.top();
		stos.push(i);
	}
	for(int i=0; i<n; ++i) {
		if(left[i]!=-1) ans++;
	}
	stack<int> stos2;
	vector<int> roots;
	vector<int> par(n);
	for(int i=n-1; i>=0; --i) {
		if(left[i]==i || left[i]==-1) continue;
		while(stos2.size() && left[stos2.top()] > i) stos2.pop();
		if(stos2.empty()) par[i]=M;
		else par[i] = stos2.top();
		stos2.push(i);
		if(par[i]==M) roots.push_back(i);
		else g[par[i]].push_back(i);
	}
	for(auto r : roots) {
		dfs(r);
		pq.push({h[r].first, r});
	}
	while(d > 0) {
		auto[ile, v] = pq.top(); pq.pop();
		best += ile;
		add_roots(v);
		--d;
	}
	cout << ans-best << "\n";

	return 0;
}
#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...