Move Bedroom
Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 18400 Accepted Submission (s): 6227
Problem description Move bedroom is very tired, XHD deep have experience. The date of the July 9, 2006, the day Xhd forced to move from building 27th to building 3rd, because 10th to seal the building. Looking at the bedroom n items, XHD began to Daze, Because n is an integer less than 2000, it is too much, so XHD decided to move 2*k pieces of the past on the line. But still very tired, because K is also not a small integer that is not greater than N. Fortunately, XHD based on years of experience in moving things found that each time the fatigue is in direct proportion to the weight difference of the right hand goods (add a sentence, xhd each move two things, left hand one right). For example, Xhd the left hand with a weight of 3 items, The right hand takes a weight of 6 items, then he moved the fatigue of this time for (6-3) ^2 = 9. Now poor XHD want to know what is the best condition (the lowest fatigue) after the 2*k, please tell him.
Input data for each group has two rows, the first row has two number n,k (2<=2*k<=n<2000). The second line has n integers representing the weight of the n item (the weight is a positive integer less than 2^15).
Output corresponds to each set of input data, and only one line of data that represents his minimum fatigue.
Sample Input
2 11 3
Sample Output
4
Authorxhd
SOURCEACM Summer Training Team Practice Competition (ii)
Recommendlcy | We have carefully selected several similar problems for you:1058 1257 2084 2571 1978
After sorting, the squared difference of the adjacent number must be the smallest, setting dp[i][j] to indicate the least fatigue when the first I number of J pairs
#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 ma In () {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] = d P[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;}
hdu1421--Move Bedroom