HDU-1421 Moving the dormitory【dp】

Question link: https://vjudge.net/contest/214662#problem/E

Topic meaning:

                                                                             move bedroom

Problem Description
It is very tiring to move the dormitory, xhd has a deep understanding. The time recalls July 9, 2006, that day xhd had no choice but to move from Building 27 to Building 3, because Building 10 was going to be closed. Looking at the dormitory in the bedroom For n items, xhd starts to be in a daze, because n is an integer less than 2000, which is too much, so xhd decides to just move 2*k items in the past. But it will still be very tiring, because 2*k is not small. An integer not greater than n. Fortunately, based on years of experience in moving things, xhd found that the fatigue degree of each move is proportional to the square of the weight difference between the left and right-handed items (here to add, xhd moves two things each time, One for the left hand and one for the right. For example, if xhd takes 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 poor xhd Want to know what the best state is after moving these 2*k items (that is, the lowest fatigue level), please tell him.
 
Input
Each set of input data has two lines, the first line has two numbers n, k (2<=2*k<=n<2000). The second line has n integers representing the weight of n items respectively (weight is a A positive integer less than 2^15).
Output
Corresponding to each set of input data, there is only one output data representing his minimum fatigue level, each row.
Sample Input
2 1
1 3
Sample Output
4
 
Problem- solving analysis:
Since the weight of these things can be disrupted, we might as well sort them, then it is definitely best to take two adjacent pieces, but what about taking two pieces? (an item can have two neighbors), so a decision occurs when you take the current pair, you can choose not to take this pair, then the current optimal solution is the same as the optimal solution at i-1, or Take this pair, then take the current pair on the basis of dp[i-1][j-2], which effectively avoids taking an item repeatedly.
 
#include <iostream>
#include <algorithm>  
#define MAXN 2010  
#define INF 0x3f3f3f3f    
using namespace std;
int ans[MAXN];
int dp[MAXN][MAXN];
int main()
{
    int n, k;
    while (cin >> n >> k) {
        ans[0] = 0;
        for (int i = 1; i <= n; ++i)
            cin >> ans[i];
        sort(years, years + n + 1 );
        dp[0][0] = 0;
        for (int i = 0; i <= n; ++i) {
            for (int j = 1; j <= k; ++j)
                dp[i][j] = INF;
        }
        for (int i = 2; i <= n; ++i) {
            for (int j = 1; 2 * j <= i; ++j) {
                dp[i][j] = min(dp[i - 1][j], dp[i - 2][j - 1] + (ans[i] - ans[i - 1])*(ans[i] - ans[i - 1]));
            }        // dp[i][j] represents the minimum exhaustion value for picking j pairs from the previous i items 
        }
        cout << dp[n][k] << endl;
    }
    return 0;
}

 

2018-05-01

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325128589&siteId=291194637