UV problem 10271 chopsticks (chopsticks)

Source: Internet
Author: User
// 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 ();}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.