POI2011Lightning Conductor

POI #Year2011 #dp #决策单调性

\(dp_i=\max\limits_{j=1}^{i-1}{a_j+\sqrt{i-j})}\)

\(w(j,i)=\sqrt{i-j}\) 满足四边形不等式

所以这个 \(dp\) 具有决策单调性,分治维护

// Author: xiaruize
const int N = 5e5 + 10;

int n;
int a[N];
double dp[N];

double w(int x, int y)
{
	return (double)a[x] + sqrt(y - x);
}

void calc(int l, int r, int ll, int rr)
{
	// debug(l, r, ll, rr);
	if (l > r)
		return;
	int mid = l + r >> 1;
	double mx = 0;
	int pos = 0;
	rep(i, ll, min(mid, rr))
	{
		if (w(i, mid) > mx)
		{
			mx = w(i, mid);
			pos = i;
		}
	}
	dp[mid] = max(mx, dp[mid]);
	calc(l, mid - 1, ll, pos);
	calc(mid + 1, r, pos, rr);
}

void solve()
{
	cin >> n;
	rep(i, 1, n) cin >> a[i];
	calc(1, n, 1, n);
	debug("flag");
	reverse(a + 1, a + n + 1);
	reverse(dp + 1, dp + n + 1);
	calc(1, n, 1, n);
	per(i, n, 1)
	{
		// debug(dp[i]);
		cout << (int)(ceil(dp[i])) - a[i] << endl;
	}
}

#ifndef ONLINE_JUDGE
bool end_of_memory_use;
#endif

signed main()
{
	// freopen(".in","r",stdin);
	// freopen(".out","w",stdout);
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int testcase = 1;
	// cin >> testcase;
	while (testcase--)
		solve();
#ifndef ONLINE_JUDGE
	cerr << "Memory use:" << (&end_of_memory_use - &start_of_memory_use) / 1024.0 / 1024.0 << "MiB" << endl;
	cerr << "Time use:" << (double)clock() / CLOCKS_PER_SEC * 1000.0 << "ms" << endl;
#endif
	return 0;
}
posted @ 2024-04-20 16:48  xiaruize  阅读(11)  评论(0)    收藏  举报