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