Move dormitory
Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 17885 accepted submission (s): 6065
Problem description it is very tiring to move the bedroom, xhd deep experience. time to record July 9, 2006, xhd was forced to move from building 27 to building 3 that day, because building 10 had to be closed. looking at the N items in the dormitory, xhd started to get in a daze. Because N is an integer smaller than 2000, it is too much. Therefore, xhd decided to move 2 * k items at will. but it will still be very tired, because 2 * K is not small is an integer not greater than N. fortunately, based on years of experience in moving things, xhd finds that the fatigue of each moving is directly proportional to the square of the weight difference between the items in the left and right hands (here, xhd moves two things each time, one on the left and one on the right ). for example, if xhd has an item with a weight of 3 in the left hand and an item with a weight of 6 in the right hand, then the fatigue of the item is (6-3) ^ 2 = 9. now, poor xhd wants to know what the best status (that is, the lowest fatigue) will be after these 2 * k items are moved. Please tell him.
Each input group has two rows. The first row has two numbers, N and K (2 <= 2 * k <= n <2000 ). the second row has n integers representing the weight of N items (weight is a positive integer smaller than 2 ^ 15 ).
Output corresponds to each group of input data, and only one output data indicates its minimum fatigue, each row.
Sample input2 11 3
Sample output4 DP, and slag-on code =
# Include <iostream> # include <cstdio> # include <algorithm> using namespace STD; # define min (a, B) (a) <(B )? (A) :( B) # define INF 0x7ffffff # define n 2002int A [n]; int DP [N] [N]; // DP [I] [J] indicates the minimum value of int main () {int N, K, I, J; while (scanf ("% d", & N, & K )! = EOF) {for (I = 1; I <= N; I ++) {scanf ("% d", & A [I]);} sort (a + 1, A + n + 1); // because the squared difference is the least, the order is considered. Each time two adjacent for (I = 1; I <= N; I ++) {DP [I] [0] = 0; // before initialization, the minimum value of 0 for moving I items is 0, the other values are for (j = 1; j <= N; j ++) {DP [I] [J] = inf ;}} for (I = 1; I <= N; I ++) {for (j = 0; j <= I; j + = 2) {DP [I] [J] = min (DP [I-1] [J], DP [I-2] [J-2] + (A [I]-A [I-1]) * (a [I]-A [I-1]);} cout <DP [N] [2 * k] <Endl;} return 0 ;}
DP [HDU 1421] Move to dormitory