标签:include turned ext gre cstring pac enc nim 数组
Input
Output
Sample Input
4 6 1 1 5 1 3 1 2
Sample Output
1
题目大意:给成一组多米诺牌,每个多米诺牌由上面和下面两组数组成,现要求可以翻动
颠倒上下,使得多米诺上边的点数和减去下边的点数和的绝对值最小。
题解:dp,背包,翻转或者不翻转,然后f[i][j],j表示反转后差为j的最小次数。
1 #include<iostream> 2 #include<cstring> 3 #include<cstdio> 4 #include<algorithm> 5 #include<cmath> 6 #define ll long long 7 #define inf 1000000007 8 9 using namespace std; 10 11 int n; 12 int a[1005][2]; 13 int f[1005][10005]; 14 15 int main() 16 { 17 scanf("%d",&n); 18 for(int i=1;i<=n;i++) 19 scanf("%d%d",&a[i][0],&a[i][1]); 20 memset(f,127,sizeof(f)); 21 f[0][5000]=0; 22 for(int i=0;i<n;i++) 23 for(int j=0;j<=10000;j++) 24 if(f[i][j]<inf) 25 { 26 int x1=a[i+1][0],x2=a[i+1][1]; 27 f[i+1][j+x1-x2]=min(f[i][j],f[i+1][j+x1-x2]); 28 f[i+1][j+x2-x1]=min(f[i][j]+1,f[i+1][j+x2-x1]); 29 } 30 for(int i=0;i<=5000;i++) 31 if(f[n][5000+i]<inf||f[n][5000-i]<inf) 32 { 33 printf("%d\n",min(f[n][5000+i],f[n][5000-i])); 34 break; 35 } 36 }
标签:include turned ext gre cstring pac enc nim 数组
原文地址:http://www.cnblogs.com/fengzhiyuan/p/7678262.html