HDU --- (1421) Move to dormitory (DP)

Source: Internet
Author: User
Move dormitory

Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 17687 accepted submission (s): 6000


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

 

Authorxhd

 

Source ACM summer training team exercise session (2) transfer equation: DP [I] [J] = min {DP [I-1] [J], DP [I-2] [J-1] + (AA [I]-aa [I-1]) * (AA [I]-aa [I-1])}; Code:
 1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #include<iostream> 5 using namespace std; 6 const int maxn=2002; 7 const int inf=0x3f3f3f3f; 8 int aa[maxn],dp[maxn][maxn]; 9 int n,k;10 int main()11 {12   while(scanf("%d%d",&n,&k)!=EOF)13   {14       for(int i=1;i<=n;i++)15       scanf("%d",aa+i);16      sort(aa+1,aa+n+1);17      for(int i=1;i<=n;i++)18      {19       for(int j=1;j<=k;j++)20          dp[i][j]=inf;21      }22        dp[0][0]=0;23     for(int i=2;i<=n;i++)24     {25       for(int j=1;2*j<=i;j++){26        dp[i][j]=min(dp[i-1][j],dp[i-2][j-1]+(aa[i]-aa[i-1])*(aa[i]-aa[i-1]));27       }28     }29     printf("%d\n",dp[n][k]);30   }31   return 0;32 }
View code

It is optimized to compress space because only DP [I-1], DP [I-2], DP [I]; so remove other space

Code:

 1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #include<iostream> 5 using namespace std; 6 const int maxn=2002; 7 const int inf=0x3f3f3f3f; 8 int aa[maxn],dp[3][maxn]; 9 int n,k;10 int main()11 {12   while(scanf("%d%d",&n,&k)!=EOF)13   {14       for(int i=1;i<=n;i++)15       scanf("%d",aa+i);16      sort(aa+1,aa+n+1);17      for(int i=0;i<=2;i++)18      {19       for(int j=1;j<=k;j++)20          dp[i][j]=inf;21      }22        dp[0][0]=0;23     for(int i=2;i<=n;i++)24     {25       for(int j=1;2*j<=i;j++){26        dp[i%3][j]=min(dp[(i-1)%3][j],dp[(i-2)%3][j-1]+(aa[i]-aa[i-1])*(aa[i]-aa[i-1]));27       }28     }29     printf("%d\n",dp[n%3][k]);30   }31   return 0;32 }
View code

 

HDU --- (1421) Move to dormitory (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.