Move Bedroom
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 Input2 11 3
Sample Output4
1#include <cstdio>2#include <algorithm>3 using namespacestd;4 5 Const intinf=0x3f3f3f3f;6 intdp[2005][1005];7 intv[2005];8 9 intMain ()Ten { One intn,k,i,j; Adp[0][0]=0; - while(SCANF ("%d%d", &n,&k)! =EOF) - { the for(i=1; i<=n;i++) -scanf"%d",&v[i]); - for(i=0; i<=n;i++) - for(j=1; j<=k;j++) +dp[i][j]=inf; -Sort (v,v+n+1); + for(i=2; i<=n;i++) A { at for(j=1; j*2<=i;j++) -Dp[i][j]=min (dp[i-2][j-1]+ (v[i]-v[i-1]) * (v[i]-v[i-1]), dp[i-1][j]); - } -printf"%d\n", Dp[n][k]); - } - return 0; in}
HDU 1421 Move Bedroom