hdu-1421-move-dorm-dp-java

move bedroom

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 31472    Accepted Submission(s): 10770


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
 

Author
xhd
 

Source
ACM Summer Training Team Practice Game (2)


Problem- solving ideas:
                   Sort first because fatigue = the square of the difference in weight. If you want to be smaller, it must be the difference between adjacent and recent
                   ones. dp Take j for the first i item. Pay attention to a problem with the minimum fatigue. If you want to use the i-th item, the i-1-th item must be unused, so you have to go back two spaces

import java.util.Arrays;
import java.util.Scanner;

public class Main{

    public static void main(String [] args) {
        // TODO Auto-generated method stub
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            int n = scanner.nextInt();
            int k = scanner.nextInt() ;
            int [] arr = new int [n+1];
            arr[0] = 0;
            for (int i = 1; i < arr.length; i++) {
                arr[i] = scanner.nextInt();
            }
            Arrays.sort(arr);
            int [][] dp = new int [n+1][k+1];
            for (int i = 2; i <= n; i++) {
                for (int j = 1; j <= k && j <= i/2; j++) {
                    if (j > (i-1)/2) {
                        dp[i-1][j] = Integer.MAX_VALUE;
                    }
                    dp[i][j] = Math.min(dp[i-1][j], dp[i-2][j-1]+(arr[i]-arr[i-1])*(arr[i]-arr[i-1]));
                }
            }
            System.out.println(dp[n][k]);
        }
    }

}


Guess you like

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