# HDU 1500 (Classic disgusting DP)

Source: Internet
Author: User

This is a classic DP, but it is obviously difficult to be certain.

In the best grouping scheme, two shorter chopsticks in each group must be adjacent to each other in the sequence of chopsticks.
DP [J] [I] indicates that J groups are picked out from the front I chopsticks (the third group is not considered)
DP [J] [I] = min {
DP [J] [I-1]; (I> J * 2)
DP [J-1] [I-2] + (hash [I]-Hash [I-1]) ^ 2; (n-I> (k-j) * 3)
}

It's so disgusting. I'm so depressed that I can stick my memo to the code.

# Include <iostream>
# Include <algorithm>
# Include <functional>
Using namespace STD;
Long hash [5010];
Long DP [2, 1010] [2, 5010];
Int main ()
{
Long T, k, n;
Scanf ("% lD", & T );
While (t --)
{
Scanf ("% LD % lD", & K, & N );
Long I, J;
For (I = 0; I <n; ++ I)
{
Scanf ("% lD", & hash [I]);
}
Sort (hash, hash + N, greater <long> ());
Long T = hash [1]-Hash [2];
DP [0] [2] = T * t;

For (I = 3; I <n; ++ I)
{
T = hash [I]-Hash [I-1];
T * = T;
DP [0] [I] = DP [0] [I-1]> T? T: DP [0] [I-1];
}

For (I = 1; I <K + 8; ++ I)
{
Long S = 3 * I + 2;
T = hash [s]-Hash [s-1];
T * = T;

DP [I] [s] = DP [I-1] [S-2] + T;
For (j = S + 1; j <n; ++ J)
{
T = hash [J]-Hash [J-1];
T * = T;
T = DP [I-1] [J-2] + T;

DP [I] [J] = DP [I] [J-1]> T? T: DP [I] [J-1];
}
}
Printf ("% LD \ n", DP [K + 7] [N-1]);

}
Return 0;
}

Related Keywords:

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.