제출 #871458

#제출 시각아이디문제언어결과실행 시간메모리
871458Cyber_WolfThe short shank; Redemption (BOI21_prison)C++17
35 / 100
957 ms524288 KiB
#include <bits/stdc++.h>

using namespace std;

#define lg long long 
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);

const lg N = 5e5+5;

lg n, d, tim;
lg dp[2][N], p[N];
lg t[N], lst[N];
vector<lg> o[N];

lg C(lg i, lg j)
{
	lg ans = o[j].size()-(lower_bound(o[j].begin(), o[j].end(), i)-o[j].begin());
	return ans;
}

void compute(lg lev, lg l, lg r, lg optl, lg optr)
{
	if(l > r)	return;
	lg mid = (l+r)/2;
	array<lg, 2> best = {1000000000000, optl};
	for(int i = optl; i <= min(optr, mid-1); i++)
	{
		best = min(best, {dp[(lev+1)&1][i]+C(i+1, mid), i});
	}
	dp[lev&1][mid] = best[0];
	compute(lev, l, mid-1, optl, best[1]);
	compute(lev, mid+1, r, best[1], optr);
	return;
}

int main()
{
	fastio;
	cin >> n >> d >> tim;
	for(int i = 1; i <= n; i++)	
	{
		cin >> t[i];
	}
	for(int i = 1; i <= n; i++)
	{ 
		for(int j = i; j >= 1; j--)
		{
			if(t[j]+i-j <= tim)
			{
				lst[i] = j;
				break;
			}
		}
		p[lst[i]]++;
		p[i]--;
		o[i].push_back(lst[i]);
		for(auto it : o[i-1])	o[i].push_back(it);
		sort(o[i].begin(), o[i].end());
	}
	// for(int i = 1; i <= n; i++)	
	// {
		// p[i] += p[i-1];
		// cout << p[i] << ' ';
	// }
	// cout << '\n';
	// for(int i = 1; i <= n; i++)
	// {
		// for(int j = i; j <= n; j++)
		// {
			// if(j-i+1-max(p[j]-p[i-1], 0ll) != C(i, j))
			// {
				// cout << "A\n";
				// cout << i << ' ' << j << '\n';
				// cout << j-i+1-max(p[j]-p[i-1], 0ll) << ' ' << C(i, j) << '\n';
			// }
		// }
	// }
	d = min(n, d);
	t[0] = 1e18;
	memset(dp, 0x3f, sizeof(dp));
	dp[d&1][0] = 0;
	for(int i = 1; i <= n; i++)	dp[d&1][i] = C(1, i);
	for(int i = d-1; i >= 0; i--)
	{
		for(int j = 1; j <= n; j++)	dp[i&1][j] = 1e15;
		compute(i, 1, n, 1, n);
	}
	cout << dp[0][n] << '\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...