"ASC 23" G. acdream 1429 rectangular Polygon--DP

Source: Internet
Author: User

Test instructions: There are a lot of sticks, which pick two sticks from the stick to set them up and equal them, and ask for the maximum number of sticks to get.

Solution: It is easy to see that there is a multi-stage decision-making process, for each stick, we can not choose, or select the first set, or select the second set of the three decision. Because the last and equal of the two sets, so that one set is positive and the other is negative, then finally and 0, we use the amount of offset 0 as one of the states.

DP[I][J] represents the maximum number of sticks with the first I offset of J, since each stick is up to 200, so the offset is up to +-20000, so the enumeration between +-20000, up to 100*40000

Code:

#include <iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<cmath>#include<algorithm>#include<string>#include<vector>using namespacestd;#defineN 30007intdp[ the][60004],path[ the][60004];inta[ the];intMain () {Freopen ("polygon.in","R", stdin); Freopen ("Polygon.out","W", stdout); intn,i,j;  while(SCANF ("%d", &n)! =EOF) {memset (DP,-1,sizeof(DP)); intsum =0;  for(i=1; i<=n;i++) {scanf ("%d",&A[i]); Sum+=A[i]; }        intLow = n-sum, high = n+sum; dp[0][n] =0;  for(i=1; i<=n;i++) {             for(j=low;j<=high;j++) {                if(dp[i-1][J]! =-1&& Dp[i][j] < dp[i-1][j]) {Dp[i][j]= dp[i-1][j]; PATH[I][J]=J; }                if(dp[i-1][j-a[i]]! =-1&& Dp[i][j] < dp[i-1][j-a[i]]+1) {Dp[i][j]= dp[i-1][j-a[i]]+1; PATH[I][J]= JA[i]; }                if(dp[i-1][j+a[i]]! =-1&& Dp[i][j] < dp[i-1][j+a[i]]+1) {Dp[i][j]= dp[i-1][j+a[i]]+1; PATH[I][J]= j+A[i]; }}} printf ("%d\n", Dp[n][n]); intnow =N,pre; Vector<int>Up,down;  for(i=n;i>=1; i--) {Pre=Path[i][now]; if(Now > Pre) Up.push_back (now-pre); if(Pre > Now) Down.push_back (pre-Now ); now=Pre; }        intx =0, y =0;  for(i=0; I<up.size (); i++) {printf ("%d%d\n", x, y); X+=Up[i]; printf ("%d%d\n", x, y); Y++; }         for(i=0; I<down.size (); i++) {printf ("%d%d\n", x, y); X-=Down[i]; printf ("%d%d\n", x, y); Y++; }    }    return 0;}
View Code

"ASC 23" G. acdream 1429 rectangular Polygon--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.