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 }