P4983 immersed

Title Description

Your \ (npy \) for your nausea, specially invited four great God and a spicy chicken!

\ (\ rm hdxrie \) : "We have to sum." So with \ (\ SUM \ limits_. 1 = {I}} ^ {n-x_i \) .

\ (\ rm Imagine \) said: "We have to have the average." So with \ (\ the X-bar \) .

\ (\ rm TimeTraveller \) said: "We have to have Math." So had some disgusting combination.

\ (\ rm Althen · Way · Satan \) , said: "We have to have the square." So we squared it.

Most garbage \ (\ rm ZredXNy \) said: "Then I'll help you put it all together."

So, we got such a formula \ (: \)

\(\frac{\left((\sum\limits_{i=1}^{n}x_i×\bar x)+\bar x\right)^2}{\bar x^2}\)

We define a sequence of values that, where \ (n-\) the number of elements for this sequence.

We given a length of \ (n-\) sequence now requires it into \ (m \) segments, each segment requires the sum of the minimum value, the minimum value is obtained.

answer

This question is quite similar to the P5308 [COCI2019] Quiz.

First seen into \ (k \) segment, it is natural to think of \ (WQS \) half.

\ (\ frac {\ left ( (\ sum \ limits_ {i = 1} ^ {n} x_i × \ bar x) + \ bar x \ right) ^ 2} {\ bar x ^ 2} \) Simplification about can be equal to \ ((\ sum_. 1 = {I}} ^ {n-+ x_i. 1) ^ 2 \) .

Set \ (F_i \) represents the section \ (I \) minimum value and the number, it is easy to obtain transfer equation \ (F_i = min (F_i, F_j + (S_I - S_j +. 1) ^ 2) \) .

Suppose \ (k <j <i \ ) and \ (J \) than \ (K \) optimization, can be simplified to give \ (\ frac {f_j + s_j ^ 2 - (f_k + s_k ^ 2)} {s_j - } S_k <= 2 * (S_I +. 1) \) .

Then \ (WQS \) half like.

#include <iostream>
#include <cstdio>
#define ll long long
using namespace std;
const int N = 1e5 + 5;
const int ll inf = 1e15;
int n, m, a[N], g[N], q[N];
ll ans, f[N], s[N];
inline int read()
{
	int x = 0, f = 1; char ch = getchar();
	while(ch < '0' || ch > '9') {if(ch == '-') f = -1; ch = getchar();}
	while(ch >= '0' && ch <= '9') {x = (x << 3) + (x << 1) + (ch ^ 48); ch = getchar();}
	return x * f;
}
ll Y(int x) {return f[x] + s[x] * s[x];}
ll X(int x) {return s[x];}
double slope(int i, int j) {return (double)(Y(i) - Y(j)) / (double)(X(i) - X(j));}
int check(ll mid)
{
	int l, r; q[l = r = 1] = 0;
	for(int i = 1; i <= n; i ++)
	{
		while(l < r && slope(q[l], q[l + 1]) <= 2.0 * (double)(s[i] + 1.0)) l ++;
		f[i] = f[q[l]] + (s[i] - s[q[l]] + 1) * (s[i] - s[q[l]] + 1) + mid; g[i] = g[q[l]] + 1;// + mid
		while(l < r && slope(q[r], q[r - 1]) >= slope(q[r - 1], i)) r --;
		q[++ r] = i;
	}
	return g[n] >= m;
}
void work()
{
	n = read(); m = read();
	for(int i = 1; i <= n; i ++) {a[i] = read(); s[i] = s[i - 1] + a[i];}
	ll l = -inf, r = inf;
	while(l <= r)
	{
		ll mid = (l + r) >> 1;
		if(check(mid)) ans = mid, l = mid + 1;
		else r = mid - 1;
	}
	check(ans); printf("%lld\n", f[n] - ans * m);
}
int main() {return work(), 0;}

Guess you like

Origin www.cnblogs.com/Sunny-r/p/12625121.html