比赛 4043级NOIP2022欢乐赛5th 评测结果 AAAAAAAAAA
题目名称 地图着色 最终得分 100
用户昵称 HeSn 运行时间 1.095 s
代码语言 C++ 内存使用 74.90 MiB
提交时间 2022-11-14 21:55:39
显示代码纯文本
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int MAXN = 3010;
int n, m, a[MAXN], b[MAXN];
int f[MAXN][MAXN];
int dist(int l, int r) {
	int mid = (l + r) / 2;
	if((r - l + 1) % 2 == 0) {
		int x = (a[mid] + a[mid + 1]) / 2;
		return b[r] - b[mid] - x * (r - mid) + b[l - 1] - b[mid] + x * (mid - l + 1);
	}
	else {
		return b[r] - b[mid - 1] - a[mid] * (r - mid + 1) + b[l - 1] - b[mid - 1] + a[mid] * (mid - l);
	}
}
signed main() {
	freopen("map.in", "r", stdin);
	freopen("map.out", "w", stdout);
	cin >> n >> m;
	for(int i = 1; i <= n; i ++) {
		cin >> a[i];
	}
	sort(a + 1, a + n + 1);
	for(int i = 1; i <= n; i ++) {
		b[i] = b[i - 1] + a[i];
	}
	memset(f, 0x3f, sizeof(f));
	f[0][0] = 0;
	for(int k = 1; k <= m; k ++) {
		for(int i = 1; i <= n; i ++) {
			for(int j = 1; j <= i; j ++) {
				f[k][i] = min(f[k][i], f[k - 1][j - 1] + dist(j, i));
			}
		}
	}
	cout << f[m][n];
    return 0;
}