搬寝室
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 18400 Accepted Submission(s): 6227
Problem Description
搬寝室是很累的,xhd深有体会.时间追述2006年7月9号,那天xhd迫于无奈要从27号楼搬到3号楼,因为10号要封楼了.看着寝室里的n件物品,xhd开始发呆,因为n是一个小于2000的整数,实在是太多了,于是xhd决定随便搬2*k件过去就行了.但还是会很累,因为2*k也不小是一个不大于n的整数.幸运的是xhd根据多年的搬东西的经验发现每搬一次的疲劳度是和左右手的物品的重量差的平方成正比(这里补充一句,xhd每次搬两件东西,左手一件右手一件).例如xhd左手拿重量为3的物品,右手拿重量为6的物品,则他搬完这次的疲劳度为(6-3)^2 = 9.现在可怜的xhd希望知道搬完这2*k件物品后的最佳状态是怎样的(也就是最低的疲劳度),请告诉他吧.
Input
每组输入数据有两行,第一行有两个数n,k(2<=2*k<=n<2000).第二行有n个整数分别表示n件物品的重量(重量是一个小于2^15的正整数).
Output
对应每组输入数据,输出数据只有一个表示他的最少的疲劳度,每个一行.
Sample Input
2 1 1 3
Sample Output
4
Author
xhd
Source
Recommend
排序以后,相邻的数的平方差一定最小,设dp[i][j]表示前i个数选j对时产生的最少疲劳度
#include <map>
#include <set>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 2010;
int dp[N][1010];
int th[N];
int main()
{
int n, k;
while (~scanf("%d%d", &n, &k))
{
memset (dp, 0, sizeof(dp));
for (int i = 1; i <= n; ++i)
{
scanf("%d", &th[i]);
}
sort (th + 1, th + n + 1);
for (int i = 1; i <= n; ++i)
{
for (int j = 1; 2 * j <= i; ++j)
{
if (i == 2 * j)
{
dp[i][j] = dp[i - 2][j - 1] + (th[i] - th[i - 1]) * (th[i] - th[i - 1]);
}
else if (i > 2 * j)
{
dp[i][j] = min(dp[i - 1][j], dp[i - 2][j - 1] + (th[i] - th[i - 1]) * (th[i] - th[i - 1]));
}
}
}
printf("%d\n", dp[n][k]);
}
return 0;
}