HDU 1421 Moving Dorm DP

Source: Internet
Author: User

Move BedroomTime limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 18571 Accepted Submission (s): 6290


Problem description Move bedroom is very tired, XHD deep have experience. The date of the July 9, 2006, the day Xhd forced to move from building 27th to building 3rd, because 10th to seal the building. Looking at the bedroom n items, XHD began to Daze, Because n is an integer less than 2000, it is too much, so XHD decided to move 2*k pieces of the past on the line. But still very tired, because K is also not a small integer that is not greater than N. Fortunately, XHD based on years of experience in moving things found that each time the fatigue is in direct proportion to the weight difference of the right hand goods (add a sentence, xhd each move two things, left hand one right). For example, Xhd the left hand with a weight of 3 items, The right hand takes a weight of 6 items, then he moved the fatigue of this time for (6-3) ^2 = 9. Now poor XHD want to know what is the best condition (the lowest fatigue) after the 2*k, please tell him.
Input data for each group has two rows, the first row has two number n,k (2<=2*k<=n<2000). The second line has n integers representing the weight of the n item (the weight is a positive integer less than 2^15).
Output corresponds to each set of input data, and only one line of data that represents his minimum fatigue.
Sample Input
2 11 3

Sample Output
4


#include <iostream> #include <cstring> #include <algorithm>using namespace std; #define N 100000000# Define M 2010int Dp[m][m/2];int main () {    int n,k;    while (cin>>n>>k) {        int i,j;        int w[m],v[m];        for (i=1;i<=n;i++)  cin>>w[i];        Sort (w+1,w+n+1);        for (i=1;i<n;i++)  v[i]= (w[i]-w[i+1]) * (w[i]-w[i+1]);        for (i=0;i<=n;i++) {for            (j=0;j<=k;j++)                dp[i][j]=n;            dp[i][0]=0;        }        for (i=2;i<=n;i++) for            (j=1;2*j<=i;j++)            dp[i][j]=min (dp[i-1][j],dp[i-2][j-1]+v[i-1]);        cout<<dp[n][k]<<endl;    }    return 0;}


HDU 1421 Moving Dorm DP

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.