! HDU chopsticks-dp-(group problem)

Source: Internet
Author: User

Test instructions: A new type of chopsticks: Each pair has three, two short, the length of the difference should be as small as possible, a long, as long as the satisfaction is a pair of the longest on the line. Choose K pair in n chopsticks, ask for short chopsticks in each pair of length difference sum minimum

Analysis:

Group DP model. This question is similar to moving the bedroom, but the problem of each group more than a long chopsticks, so the sort of time from large to small, so that in the time of DP when the long selected into each group, the concrete how to achieve not too much.

Just as there are two ways to implement the grouping model summarized in the dorm, here are two codes. Note Some detail differences in the implementation, as well as initialization.

Combine the two questions to see a deeper understanding.

Code Listing 1:

#include <iostream> #include <algorithm> #include <cstdio> #include <cstring> #define MIN (b) A <b?a:b; #define INF 1000000007using namespace Std;int t,k,n;int dp[5010][1100],a[5100];bool cmp (int i,int j) {return i& Gt;j;} int main () {cin>>t;while (t--) {cin>>k>>n;for (int i=1;i<=n;i++) Cin>>a[i];sort (a+1,a+n+1, CMP); k+=8;for (int i=0;i<=n;i++) for  (int j=1;j<=k;j++)     dp[i][j]=inf;for (int i=0;i<=n;i++) dp[i][0] =0;for (int i=3;i<=n;i++) {for (int j=1;j*3<=i;j++) {    dp[i][j]=min (dp[i-1][j],dp[i-2][j-1]+ (a[i]-a[i-1)) * ( A[I]-A[I-1]));}} Cout<<dp[n][k]<<endl;}}
Code Listing 2:

#include <iostream> #include <algorithm> #include <cstring> #define MIN (b) a<b?a:b;using namespace Std;int t,k,n;int dp[1010][5100],a[5100];bool cmp (int i,int j) {return i>j;} int main () {cin>>t;while (t--) {cin>>k>>n;for (int i=1;i<=n;i++) Cin>>a[i];sort (a+1,a+n+1, CMP), memset (Dp,0,sizeof (DP)), k+=8;for (int i=1;i<=k;i++) {dp[i][3*i]=dp[i-1][3*i-2]+ (a[3*i]-a[3*i-1]) * (a[3*i]- A[3*I-1]); for (int j=3*i+1;j<=n;j++)    dp[i][j]=min (dp[i][j-1],dp[i-1][j-2]+ (a[j]-a[j-1]) * (A[j]-a[j-1]);} Cout<<dp[k][n]<<endl;}}



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

! HDU chopsticks-dp-(group problem)

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.