HDU-1421--Move the bedroom

Title description:

Moving the dormitory is very tiring, and xhd has a deep experience. Time goes back to July 9, 2006, that day xhd was forced to move from building 27 to building 3 because of the closure of the building on the 10th. Looking at the dormitory. n items, xhd began to be in a daze, because n is an integer less than 2000, it is too much, so xhd decided to move 2 k pieces in the past. But it will still be very tired, because 2 k is not too small, it is not a no An integer greater than n. Fortunately, based on years of experience in moving things, xhd finds that the fatigue of each move is proportional to the square of the weight difference between the left and right-handed items. For example, if xhd holds an item with a weight of 3 in his left hand and an item with a weight of 6 in his right hand, his fatigue level after moving this time is (6-3)^2 = 9. Now the poor xhd wants to know What is the best condition after moving these 2*k items (that is, the lowest fatigue), please tell him.

enter:

Each set of input data has two rows, the first row has two numbers n, k(2<=2*k<=n<2000). The second row has n integers representing the weight of n items (the weight is one A positive integer less than 2^15).

Output:

Corresponding to each set of input data, there is only one output data that represents his least fatigue, each row.

Ideas:

  • The analysis shows that if the fatigue value is to be minimized, then the items moved each time must have the smallest weight difference, so the data must be sorted first.
  • Use a two-dimensional array dp[ i ][ j] to represent the minimum fatigue of the first i items moved j times. First of all, we can know that the first two items are moved once, and the minimum fatigue is the square of the difference between the first two weights, then the first 3 How about moving each item at once? At this time, the advantages of dynamic programming are reflected. The third item can be selected or not. If not selected, it is the result of moving the first two items once. It has already been calculated. There is no need to repeat the calculation. If you choose , Then it must be the square of the difference between the second and third items, and so on. For the first 4 items, find the smallest value of the first 3 items and the third and fourth items, whichever is smaller, until the nth item
  • Then move twice. If you move twice, you must start with 4 pieces. The minimum value of moving 4 pieces twice is 1 and 2, 3 and 4, so what about moving 5 pieces twice? If you do not choose the 5th item, the answer is to move the first 4 items twice. If you choose, it is to move the first three items once plus the 4th and 5th items. Until n pieces are moved k times
  • Transfer equation:
    dp[j][i]=min(dp[j-1][i],dp[j-2][i-1]+pow(a[j]-a[j-1],2));

Code:

#include <iostream>
#include <cstring>
#include <algorithm> 
#include <cmath>
using namespace std;
int n,k,ans;
int a[2005];
int dp[2001][1001];//dp[i][j]表示前i件物品,搬j次最低疲劳度 
int min(int x,int y)
{
    
    
	return x<y?x:y;
}
int main()
{
    
    
	while(cin>>n>>k)
	{
    
    
		ans=0;
		memset(dp,0,sizeof(dp)); 
		for(int i=1;i<=n;i++)
		{
    
    
			cin>>a[i];
		}
		sort(a+1,a+n+1);
		for(int i=1;i<=k;i++)//搬的次数 
		{
    
    
			dp[i*2][i]=dp[i*2-2][i-1]+pow(a[i*2]-a[i*2-1],2);//先计算前i*2件搬i次消耗的疲劳值
			for(int j=i*2+1;j<=n;j++)//物品数 
			{
    
    
				dp[j][i]=min(dp[j-1][i],dp[j-2][i-1]+pow(a[j]-a[j-1],2));
			}
		}
		cout<<dp[n][k]<<endl;		
	}
	
	return 0;
} 

Guess you like

Origin blog.csdn.net/weixin_45102820/article/details/114037502