记录编号 596512 评测结果 AAAAAAAAAAAAAAAAAAAAAAAAA
题目名称 [NOIP 2021]方差 最终得分 100
用户昵称 Gravatar┭┮﹏┭┮ 是否通过 通过
代码语言 C++ 运行时间 0.419 s
提交时间 2024-10-29 10:56:52 内存使用 7.36 MiB
显示代码纯文本
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int,ll>
#define fi first
#define in inline
#define se second
#define mp make_pair
#define pb push_back
const int N = 1e4+10;
const ll inf = 1e18;

ll read(){
	ll x = 0,f = 1;char c = getchar();
	for(;c < '0' || c > '9';c = getchar())if(c == '-')f = -1;
	for(;c >= '0' && c <= '9';c = getchar())x = (x<<1) + (x<<3) + c-'0';
	return x * f;
}
void cmin(ll &x,ll y){x = min(x,y);}


int n;
int a[N],c[N],s[N];
ll f[500010],mx = 0;

int main(){
	freopen("2021variance.in","r",stdin);
	freopen("2021variance.out","w",stdout);
	n = read();
	for(int i = 1;i <= n;i++){
		a[i] = read(),mx = max(mx,(ll)a[i]);
		if(i > 1)c[i-1] = a[i] - a[i-1];
	}
	sort(c+1,c+n);
	for(int i = 1;i < n;i++)s[i] = s[i-1] + c[i];
	memset(f,0x3f,sizeof f);
	f[0] = 0;
	ll ans = inf;
	for(int i = 1;i < n;i++){
		if(c[i] == 0)continue;
		for(int j = mx * n;j >= 0;j--){
			if(f[j] == 0x3f3f3f3f3f3f3f3f)continue;
			cmin(f[j + s[i]],f[j] + s[i] * s[i]);
			cmin(f[j + i * c[i]],f[j] + 2ll * j * c[i] + i * c[i] * c[i]);
			f[j] = 0x3f3f3f3f3f3f3f3f;
		}
	}
	for(int i = 0;i <= mx * n;i++)
		if(f[i] != 0x3f3f3f3f3f3f3f3f)cmin(ans,f[i] * n - (ll)i * i);
	printf("%lld\n",ans);

	return 0;

}