Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 6731 | Accepted: 2234 |
Description
The number of dots in the top line is 6+1+1+1=9 and the number of dots in the bottom line is 1+5+3+2=11. The gap between the top line and the bottom line is 2. The gap is the absolute value of difference between two sums.
Each domino can be turned by 180 degrees keeping its face always upwards.
What is the smallest number of turns needed to minimise the gap between the top line and the bottom line?
For the figure above it is sufficient to turn the last domino in the row in order to decrease the gap to 0. In this case the answer is 1.
Write a program that: computes the smallest number of turns needed to minimise the gap between the top line and the bottom line.
Input
Each of the next n lines contains two integers a, b separated by a single space, 0 <= a, b <= 6. The integers a and b written in the line i + 1 of the input file, 1 <= i <= 1000, are the numbers of dots on the i-th domino in the row, respectively, in the top line and in the bottom one.
Output
Sample Input
4
6 1
1 5
1 3
1 2
Sample Output
1
Source
题意:
多米诺骨牌有上下2个方块组成,每个方块中有1~6个点。现有排成行的
上方块中点数之和记为S1,下方块中点数之和记为S2,它们的差为|S1-S2|。例如在图8-1中,S1=6+1+1+1=9,S2=1+5+3+2=11,|S1-S2|=2。每个多米诺骨牌可以旋转180°,使得上下两个方块互换位置。
编程用最少的旋转次数使多米诺骨牌上下2行点数之差达到最小。
很普通的01背包
f[i][j+s]表示前i个差值为s的最少次数
滚动数组+for(int j=-i*6;j<=i*6;j++)轻松200ms快了两三倍
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int N=,INF=1e9;
inline int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x*f;
}
int n,w[N],s;
int f[][*N*];
void dp(){
memset(f,,sizeof(f));
f[][s]=;
for(int i=;i<=n;i++){
for(int j=-i*;j<=i*;j++){
int p=i&;
j+=s;
f[p][j]=INF;
if(j-w[i]>=) f[p][j]=min(f[p][j],f[p^][j-w[i]]);
if(j+w[i]>=&&f[p^][j+w[i]]<INF) f[p][j]=min(f[p][j],f[p^][j+w[i]]+);
j-=s;
}
}
}
int main(){
n=read();s=n*;
for(int i=;i<=n;i++) w[i]=read()-read();
dp();
int p=n&;
for(int d=;d<=n;d++){
if(f[p][s+d]!=INF){printf("%d",f[p][s+d]);break;}
if(f[p][s-d]!=INF){printf("%d",f[p][s-d]);break;}
}
}