标签:dp
2 1 1 3
4
#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; }
标签:dp
原文地址:http://blog.csdn.net/guard_mine/article/details/41958565