Your constancy, why shift up just before dawn sleep, the most useless, MO a day exposure 10th cold. "Dynamic planning" domino bone time limit: 1 Sec memory limit: MB
The title describes a domino with up to 2 blocks, each with a 1~6 point. The existing rows of n dominoes are shown.
The sum of the points in the upper block is recorded as Σ1, and the sum of the points in the lower block is recorded as Σ2, and their difference is |σ1-σ2|. For example, σ1=6+1+1+1=9,σ2=1+5+3+2=11,|σ1-σ2| = 2. Each domino can rotate 180 °, making the upper and lower two blocks swap positions.
Programming with a minimum number of rotations makes it possible to minimize the difference between 2 line points on a domino.
For the example in Figure 8-1, if the last domino is rotated by 180 °, the difference between the top and bottom 2 line points will be 0.
The first line of input is a positive integer n (1≤n≤1000), which indicates the number of dominoes. The next n rows represent the points of N dominoes. Each line has two positive integers separated by a space, indicating the points A and B in the upper and lower squares of the dominoes, and 1≤a,b≤6. Outputs only one row, containing an integer. Represents the minimum number of rotations to be evaluated. Sample input
46 11 51) 31 2
Sample output
1
Analysis: The array should be opened more than once because there are negative values on the dominoes. DP[X] Represents the minimum number of exchanges for X
Dp[j+a]=min (Dp[j+a],dp[j]);
Dp[j+b]=min (dp[j+b],dp[j]+1);
#include <iostream>#include<cstdio>#include<cstring>#include<cmath>#include<cstdlib>using namespacestd;Const intMAXN =12010;intDP[MAXN], N;intMain () {inta,b,sum=0, I,j; CIN>>N; for(intI=1; i<maxn;i++) Dp[i]=200000; dp[0]=0; for(i=1; i<=n;i++) {cin>>a>>b; Sum+=a+b; for(j=sum;j>=0; j--) { intt=Dp[j]; DP[J]=200000; Dp[j+a]=min (dp[j+a],t); Dp[j+b]=min (dp[j+b],t+1); }} sum=sum/2; for(i=sum;i>=0; i--) if(dp[i]<=N) Break; for(j=sum;j<maxn;j++) if(dp[j]<=N) Break; cout<<min (Dp[i],dp[j]) <<Endl; return 0;}
View Code
"Dynamic Programming" domino bone