// Chopsticks (chopsticks) // PC/ultraviolet A IDs: 111107/10271, popularity: B, success rate: average level: 3 // verdict: accepted // submission date: 2011-10-18 // UV Run Time: 0.100 s /// copyright (c) 2011, Qiu. Metaphysis # Yeah dot net // [solution] // set badness [I] [J] to the minimum difficulty for the combination of group I chopsticks formed by the first J chopsticks cost, if length [J] is the length of the nth/J chopsticks, the following state transition equation is available: /// badness [I] [J] = min {badness [I] [J-1], badness [I-1] [J-2] + (length [J]-// length [J-1]) * (length [J]-length [J-1) | j> = 3 * I-1} // The sequence of the length of the chopsticks (in ascending order ):////..., c1, C2, C3 ,... //// it can be proved that if C1 <C2, if C1 is used as the first chopsticks in the optimal scheme, C2 will be used as the second chopsticks. You can use the // reverse verification method. In the optimal solution, if C1 is not a group of chopsticks with C2, the use of chopsticks made up of C1 and C2 is less difficult than that of chopsticks made up of C1 // and other chopsticks, conflicts with the optimal solution. Therefore, when selecting the first and second chopsticks, you can always combine adjacent chopsticks to minimize the difficulty. # Include <iostream> using namespace STD; # define maxn 5001int length [maxn]; int badness [maxn]; int difference [maxn]; int nguests, nchopsticks; // use a one-dimensional array to optimize space usage. Int dynamic_programming () {for (INT I = 0; I <= nchopsticks; I ++) badness [I] = 0; For (INT I = 1; I <= nguests; I ++) {int setted = nchopsticks-3 * (nguests-I); For (Int J = setted-1; j> = 2 * I; j --) badness [J] = badness [J-2] + difference [J]; for (Int J = 2 * I + 1; j <= setted-1; j ++) badness [J] = min (badness [J], badness [J-1]); badness [setted] = badness [setted-1];} cout <badness [nchopsticks] <Endl;} int main (int ac, char * AV []) {INT cases; CIN> cases; while (cases --) {CIN> nguests> nchopsticks; nguests + = 8; For (INT I = 1; I <= nchopsticks; I ++) {CIN> length [I]; difference [I] = length [I]-length [I-1]; difference [I] * = difference [I];} dynamic_programming ();}}