UVA 10271--chopsticks +DP

Source: Internet
Author: User

The length of the chopsticks from long to short arrangement, you can ignore the impact of Z (for each pair (x, y) can always be guaranteed to be greater than its z);

Then define the state Dp[i][j] to represent the minimum cost of the J pair in the front I chopsticks, and then:

If J<3*i is dp[i][j]=dp[i-1][j]

If the j>=3*i for the first chopsticks can be selected to form the J pair (and i-1 chopsticks constitute the J (x, Y)), of course, can also be selected;

Then Dp[i][j]=min (Dp[i-1][j],dp[i-2][j]+w (a[i],a[i-1]))


The code is as follows:

#include <iostream> #include <cstdio> #include <cstring>using namespace std; #define INF 0x3fffffffint A[10000];int dp[5500][1100];int n,k;void input () {    scanf ("%d%d", &k,&n);    for (int i=n;i>=1;i--)        scanf ("%d", &a[i]);} void Solve () {for    (int. i=0;i<=n;i++) for      (int j=0;j<=n;j++)      {          dp[i][j]=inf;          if (j==0)            dp[i][0]=0;      }    for (int i=1;i<=n;i++) for     (int j=1;j<=k+8;j++)     {         dp[i][j]=dp[i-1][j];         if (i>=j*3)         {             dp[i][j]=min (dp[i][j],dp[i-2][j-1]+ (a[i]-a[i-1]) * (a[i]-a[i-1]));         }     }    printf ("%d\n", Dp[n][k+8]);} int main () {    int t;    scanf ("%d", &t);    while (t--)    {       input ();       Solve ();    }  return 0;} </span>



UVA 10271--chopsticks +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.