题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1421
2 1 1 3
4
#include <cstdio> #include <cmath> #include <cstring> #include <string> #include <cstdlib> #include <climits> #include <ctype.h> #include <queue> #include <stack> #include <vector> #include <deque> #include <set> #include <map> #include <iostream> #include <algorithm> using namespace std; #define PI acos(-1.0) #define INF 0x3fffffff //typedef long long LL; //typedef __int64 LL; #define maxn 2017 int dp[maxn][1017]; //前i个物品中选j对的最小疲劳度 int MIN(int a, int b) { return a<b?a:b; } int main() { int n, k; int i, j; int a[maxn]; while(~scanf("%d%d",&n,&k)) { for(i = 1; i <= n; i++) { scanf("%d",&a[i]); } sort(a+1,a+n+1); for (i = 0; i <= n; i++) { for(j = 1; j <= k; j++) dp[i][j] = INF; } for(i = 2; i <= n; i++) { for(j = 1; j*2 <= i; j++) { dp[i][j]=MIN(dp[i-2][j-1]+(a[i]-a[i-1])*(a[i]-a[i-1]),dp[i-1][j]); } } printf("%d\n",dp[n][k]); } return 0; }
hdu1421 搬寝室(dp),布布扣,bubuko.com
原文地址:http://blog.csdn.net/u012860063/article/details/38237451