poj 1717 Dominoes(dp)
来源:互联网 发布:怎么更改mac地址 编辑:程序博客网 时间:2024/05/22 04:59
Dominoes
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 6728 Accepted: 2232
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
[Submit] [Go Back] [Status] [Discuss]
题解:dp
f[i][j]表示到第i列第一行-第二行的差值为j的最少交换次数。
#include<iostream>#include<cstdio>#include<algorithm>#include<cmath>#include<cstring>#define N 1003using namespace std;int f[N][N*10],a[N],b[N],n;int main(){freopen("a.in","r",stdin);freopen("my.out","w",stdout);scanf("%d",&n);for (int i=1;i<=n;i++) scanf("%d%d",&a[i],&b[i]);int base=5000;memset(f,127/3,sizeof(f)); int inf=f[0][0];if (a[1]==b[1]) f[1][a[1]-b[1]+base]=0;else f[1][a[1]-b[1]+base]=0,f[1][b[1]-a[1]+base]=1;for (int i=2;i<=n;i++) for (int j=-5000;j<=5000;j++) { f[i][j+base]=min(f[i][j+base],f[i-1][j+b[i]-a[i]+base]); f[i][j+base]=min(f[i][j+base],f[i-1][j+a[i]-b[i]+base]+1); }int ans=1000000000,ansx=1000;for (int i=0;i<=2*base;i++) if (f[n][i]!=inf){ int t=i-base; if (ans>t) ans=min(ans,abs(t)),ansx=f[n][i]; else if (ans==t) ansx=min(ansx,f[n][i]); }printf("%d\n",ansx);}
刚开始还写了一种不科学的思路。f[i]表示到第i列取到差值最小时第一行的值,g[i]为此时对应的反转个数。这样每次枚举每一列和最近的反转列(转移时保证i反转)。但是这样是存在问题的,因为有可能存在情况,要求前面的必须差值变大,后面才能将其变小。
4
2 1
1 3
4 1
6 1
这组样例就能卡掉上面的那种方法。。。。。
0 0
- POJ 1717 Dominoes(DP)
- poj 1717 Dominoes(dp)
- 【POJ 1717】Dominoes(dp)
- poj 1717 Dominoes (dp)
- [POJ1717]Dominoes(dp)
- poj 1717 Dominoes
- poj 1717 Dominoes
- poj 1717 Dominoes
- poj 1717 Dominoes 背包
- POJ 1717 Dominoes
- Long Dominoes (状态dp)
- poj 1717 Dominoes 01背包
- zoj2563 Long Dominoes(状压dp)
- [POJ1717][luogu1282]Dominoes(多米诺骨牌)(dp)
- poj 3797 Tiling a Grid With Dominoes 状压dp
- hdu 1992 Tiling a Grid With Dominoes (DP)
- Uva - 11270 - Tiling Dominoes(轮廓线dp)
- UVA 11270 Tiling Dominoes(轮廓线DP经典)
- iOS安全攻防-键盘安全
- APM(应用性能管理)漏斗分析使用
- 面向对象程序设计上机练习九(对象指针)
- android系统镜像:boot.img kernel.img ramdisk.img system.img userdata.img cache.img recovery.img
- 元类
- poj 1717 Dominoes(dp)
- Android解决AutoLayout不能设置多种设计稿尺寸的问题
- mysql 存储过程及常用函数
- post上传表单图片
- spring 的定时器配置
- 如何调试makefile变量
- Android onCreate方法两个参数和一个参数的区别
- Java程序性能优化。
- C#获取命令行输出内容