Moving Dorm (classic DP)

Source: Internet
Author: User

Topic Connection: http://acm.hdu.edu.cn/showproblem.php?pid=1421hdu_1421: Move Bedroom

Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 22007 Accepted Submission (s): 7460


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 Input2 3 Sample Output4

Authorxhd

SOURCEACM Summer Training Team Practice Competition (ii)

Problem: Classic DP question, set DP[I][J] means that I have got the item I took 2*k pieces

The transfer equation is either at the time of the first I piece including the first part I so that the i+1 pieces must be taken at this time dp[i][k] = dp[i-2][k-1]+a[i-1], or do not include the drop I pieces, then dp[i][k+1] must be equal to dp[i-1][k], two cases take the smallest

But as long as the DP must pay attention to the boundary conditions, first look at the transfer equation is what you must deal with the initial data, when k = = 0 when all the DP is 0; When the transfer of the 2*k>=i is no longer transferred, because it is impossible to appear, must think clearly when you can continue to transfer

Special note: For any of the questions, you have to understand in advance whether the number of points starting from 1 or 0, and then all the programs follow this rule to write, because the problem of the number of errors, the program is not good to change the long.

Code:

1#include <cstdio>2#include <algorithm>3#include <cstring>4 using namespacestd;5 #defineINF 0x7f7f7f7f6 #defineN 20107 intV[n];8 intA[n];9 intDp[n][n];Ten intMain () One { A     intN, K; -      while(~SCANF ("%d%d",&n,&k)) -     { the          for(intI=0; i < n; i++) scanf ("%d",&v[i]); -Sort (v,v+n); -          for(inti =1; I < n; i++) a[i-1] = (v[i]-v[i-1]) * (v[i]-v[i-1]); -         //memset (Dp,0x7f,sizeof (DP)); +          for(inti =0; I < n; i++) -              for(intj = i/2+1; j< N; j + +) Dp[i][j] =INF; +          for(intj =0; J < N; j + +) dp[j][0] =0; Adp[1][1] = a[0]; at          for(inti =2; i < n; i++)  -              for(intj =1;2*j <= i+1; j + +) -Dp[i][j] = min (dp[i-1][j],dp[i-2][j-1]+a[i-1]); -printf"%d\n", dp[n-1][k]); -     } -     return 0; in}

Moving Dorm (classic 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.