HDU-1421- conveyed bedroom -dp

Move the bedroom is very tired, xhd deep. Time traced July 9, 2006, the day xhd forced to move from Building 27, Building 3, as No. 10 to seal the floor. Looked at the bedroom n items, xhd start in a daze, because n is an integer less than 2000, it is too much, so xhd decided to just move 2 * k pieces of the past on the line, but still very tired, because 2 * k is not small n is an integer greater than Fortunately XHD each time it is found that fatigue is proportional to the square of the difference in weight of the right hand of the article (add here that, each transfer two things XHD based on years of experience of moving things, a left hand one). For example the left hand xhd weight of the article 3, the right hand of the weight of the article 6, the degree of his fatigue of the move those (6-3) ^ 2 = 9. now poor xhd move those that want to know the best condition after 2 * k what items (that is, the lowest fatigue), please tell him.

Input data each have two input lines, the first line has two numbers n, k (2 <= 2 * k <= n <2000). The second row has n each represent an integer of n items of weight (weight of a positive integer smaller than 2 ^ 15) .OUTPUT input data corresponding to each output data represents only a minimum of his fatigue, each row .Sample input

2 1
1 3

Sample Output

4


Reference article:

https://blog.csdn.net/cjf1699/article/details/77836795

 

Meaning of the questions:

  A given n and k, k elected representatives from the n items things inside, and then given weight of n items. Fatigue refers to the difference of squares of the weight of any articles two k, find the least fatigue.

Ideas:

  Fatigue is the square of the difference in weight of the two adjacent articles, a first sorted, which then re-find the k adjacent minimum difference, the difference of consecutive time, the definition of a two-dimensional array dp, for It represents how many neighbor taking the difference, how much fatigue. Each adjacent two adjacent difference can only take a.

My code:

  

 1 #include<stdio.h>
 2 #include<iostream>
 3 #include<cmath>
 4 #include<algorithm>
 5 #include<string.h>
 6 #include<iomanip>
 7 using namespace std;
 8 #define inf 0x3f3f3f3f
 9 //#define inf 0x7fffffff
10 
11 int a[2020];
12 int dp[1010][2020];
13 int main()
14 {
15     :: :: sync_with_stdio iOS STD ( to false );
 16      cin.tie ( 0 );
 . 17      cout.tie ( 0 );
 18 is      int n-, K;
 . 19      the while (CIN >> >> n-K) // note plurality of sets input otherwise there is no this would WA 
20 is      {
 21 is          Memset (A, 0 , the sizeof (A));
 22 is          Memset (DP, 0 , the sizeof (DP));
 23 is          for ( int I = . 1 ; I <= n-; I ++ )
 24              >> CIN A [I];
 25         sort(a+1,a+n+1);
26 //    for(int i=1; i<=n; i++)
27 //        dp[0][i]=0;
28         for(int i=1; i<=k; i++)
29         {
30             for(int j=2*i; j<=n; j++)
31             {
32                 if(j<2*i)
33                     continue;
34                 else if(j==2*i)
35                     dp[i][j]=inf;
36                 else if(j>2*i)
37                     dp[i][j]=dp[i][j-1];
38                 dp[i][j]=min(dp[i][j],dp[i-1][j-2]+(a[j]-a[j-1])*(a[j]-a[j-1]));
39             }
40         }
41         cout<<dp[k][n]<<endl;
42     }
43     return 0;
44 }

 

 

 

 

Guess you like

Origin www.cnblogs.com/OFSHK/p/11246766.html