Submission #521172

#TimeUsernameProblemLanguageResultExecution timeMemory
521172blueThe short shank; Redemption (BOI21_prison)C++17
100 / 100
457 ms269108 KiB
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

using vi = vector<int>;
#define sz(x) int(x.size())

vi* children;
vi parent;

vi depth;
vi st_depth;

vi occ;

void dfs1(int u)
{
	for(int v: children[u])
	{
		depth[v] = 1 + depth[u];
		dfs1(v);
		st_depth[u] = max(st_depth[u], 1 + st_depth[v]);
	}
}

void dfs2(int u, int ct)
{
	if(sz(children[u]) == 0) occ[ct]++;
	else
	{
		bool done = 0;
		for(int v: children[u])
		{
			if(!done && st_depth[v] == st_depth[u] - 1)
			{
				done = 1;
				dfs2(v, ct+1);
			}
			else
			{
				dfs2(v, 1);
			}
		}
	}
}

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);

	int N, D, T;
	cin >> N >> D >> T;

	vi t(1+N);
	for(int i = 1; i <= N; i++) cin >> t[i];

	vi J(1+N, -1); //place mattress in (J[i], i)

	vi rst;

	for(int j = N; j >= 1; j--)
	{
		rst.push_back(j);

		while(!rst.empty())
		{
			int i = rst.back();

			if(t[j] + (i - j) > T) break;

			J[i] = j;

			rst.pop_back();
		}
	}

	// for(int i = 1; i <= N; i++) cerr << i << " : " << J[i] << '\n';

	vi closings(1+N);

	int res = N;

	vi active(1+N, 0);

	for(int i = N; i >= 1; i--)
	{
		if(J[i] == i)
		{
			continue;
		}

		if(J[i] == -1)
		{
			res--;
			continue;
		}

		active[i] = 1;

		closings[ J[i] ]++;
	}



	vi st{0};

	children = new vi[1+N];
	parent = vi(1+N);

	// cerr << "res = " << res << '\n';

	for(int i = N; i >= 1; i--)
	{
		if(active[i]) 
		{
			// cerr << "active = " << J[i] << " " << i << '\n';
			parent[i] = st.back();
			children[parent[i]].push_back(i);
			st.push_back(i);
		}
		else 
		{
			for(int z = 0; z < closings[i]; z++)
				st.pop_back();
		}
 	}

 	depth = vi(1+N, 0);
 	st_depth = vi(1+N, 0);

 	occ = vi(1+N, 0);

 	dfs1(0);

 	dfs2(0, 0);

 	for(int z = N; z >= 0; z--)
 	{
 		while(occ[z] && D)
 		{
 			occ[z]--;
 			D--;
 			res -= z;
 		}
 	}

 	cout << res << '\n';
}
#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...