POJ 1717 Dominoes
来源:互联网 发布:亚马逊域名 编辑:程序博客网 时间:2024/05/21 19:37
Description
A domino is a flat, thumbsized tile, the face of which is divided into two squares, each left blank or bearing from one to six dots. There is a row of dominoes laid out on a table:
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.
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
The first line of the input contains an integer n, 1 <= n <= 1000. This is the number of dominoes laid out on the table.
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.
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
Output the smallest number of turns needed to minimise the gap between the top line and the bottom line.
Sample Input
46 11 51 31 2
Sample Output
1
Source
CEOI 1997
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~01背包~
翻车了……
记录一个maxx值表示上下最大差值。
用f[i][j]表示差值为j-maxx时的最小步数,如果f[i][j]==n表示不能得到这个差值。其中i这一维滚动。
用d(i)来表示i+maxx,结果最后求值的时候忘记了,写成了d(maxx+i)……WA到怀疑人生……所以一定要细心呀!
#include<cstdio>#include<cstring>#include<iostream>using namespace std;#define d(u) (u+maxx)int n,a[1001],maxx,f[2][20001],inf;bool kkz;int read(){int x=0,f=1;char ch=getchar();while(ch<'0' || ch>'9') {if(ch=='-') f=-1;ch=getchar();}while(ch>='0' && ch<='9') {x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}return x*f; }int main(){n=read();for(int i=1;i<=n;i++){a[i]=read()-read();maxx+=a[i]<0 ? -a[i]:a[i];}for(int i=0;i<=2*maxx;i++) f[kkz][i]=n+1;inf=n+1;f[kkz][d(a[1])]=0;f[kkz][d(-a[1])]=1;for(int i=2;i<=n;i++){kkz^=1;for(int j=0;j<=2*maxx;j++) f[kkz][j]=n+1;for(int j=0;j<=2*maxx;j++) if(f[kkz^1][j]!=inf) { f[kkz][j+a[i]]=min(f[kkz][j+a[i]],f[kkz^1][j]);f[kkz][j-a[i]]=min(f[kkz][j-a[i]],f[kkz^1][j]+1); }}for(int i=0;;i++) if(f[kkz][d(i)]!=inf || f[kkz][d(-i)]!=inf) { printf("%d\n",min(f[kkz][d(i)],f[kkz][d(-i)]));break; }return 0;}
0 0
- poj 1717 Dominoes
- poj 1717 Dominoes
- poj 1717 Dominoes
- poj 1717 Dominoes (dp)
- poj 1717 Dominoes 背包
- POJ 1717 Dominoes
- POJ 1717 Dominoes(DP)
- poj 1717 Dominoes 01背包
- poj 1717 Dominoes(dp)
- 【POJ 1717】Dominoes(dp)
- Dominoes
- poj 3797 Tiling a Grid With Dominoes 状压dp
- pku1717 Dominoes
- Tiling Dominoes
- uva11270Tiling Dominoes
- Long Dominoes
- POJ-3797(状态转移)(Tiling a Grid With Dominoes )
- Dominoes-第一个回溯
- Android国际化
- 【源码剖析】tinyhttpd —— C 语言实现最简单的 HTTP 服务器
- HDU1394 Minimum Inversion Number(线段树单点更新,暴力,逆序数)
- android设计模式-装饰模式(Decorator Pattern)
- PAT 乙等 1006 C语言
- POJ 1717 Dominoes
- Java中compareTo用法
- C++标准库类型——迭代器
- 例题6-2 UVa 514(栈)
- OpenStack Ocata 安装(七)创建虚拟网络、实例模板、实例
- Windows PE变形练手2-开发一套自己的PE嵌入模板
- 解决Android Studio运行java文件,控制台输出乱码问题
- Linux rpm/dpkg/yum/apt常用选项组合
- MongoDBPHP 扩展