【POJ 1717】Dominoes(dp)
来源:互联网 发布:mac 批量 wav 转 mp3 编辑:程序博客网 时间:2024/05/21 20:45
Dominoes
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 6729 Accepted: 2233
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]
Home Page Go Back To top
[题意][给定两排n个数,问当两排之差的和最小时,最少交换了几次]
【题解】【dp】
【一个较简单的dp,设f[i][j]为枚举到第i个数,差为j时最少交换了多少次。要考虑j为负数的情况,所以要加eps】
【方程:f[i][j]=min(f[i][j],min(f[i-1][j-(a[i]-b[i])],f[i-1][j+(a[i]-b[i])]+1)。刚开始读入时,预处理每次交换当前值的情况】
#include<cstdio>#include<cstring>#include<algorithm>#define eps 5000using namespace std;int a[1010],b[1010],n,f[1010][10010];int ans,num,sum,inf;int main(){//freopen("int.txt","r",stdin);//freopen("my.txt","w",stdout);int i,j;scanf("%d",&n); memset(f,127,sizeof(f));inf=f[0][0];for(i=1;i<=n;++i) { scanf("%d%d",&a[i],&b[i]); int x=a[i],y=b[i]; f[i][sum+x-y+eps]=0; f[i][sum+y-x+eps]=1; sum+=(x-y); } for(i=2;i<=n;++i) for(j=-5000;j<=5000;++j) { int x=a[i],y=b[i],ch=x-y,sm; sm=min(f[i-1][j-ch+eps],f[i-1][j+ch+eps]+1); f[i][j+eps]=min(f[i][j+eps],sm); }for(i=0;i<=5000;++i) if(f[n][i+eps]!=inf) { ans=f[n][i+eps]; num=i; break; }for(i=0;i>=-5000;--i) if(f[n][i+eps]!=inf) if(-i<num) ans=f[n][i+eps],num=-i; else if(-i==num) {ans=min(ans,f[n][i+eps]); break;}printf("%d\n",ans);return 0;}
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经典)
- codechef Chef and Churu
- 100天土鸡饲养计划(76)
- 打开新窗口的几种方式及target=_blank指定窗口名称
- <转>Spark Streaming编程指南
- 如何修改PDF黑色背景色和文字颜色方便打印
- 【POJ 1717】Dominoes(dp)
- git命令记录
- SQL分页
- 逻辑地址&物理地址
- IOS Xcode8 日志打印不全及屏蔽系统日志解决方法
- 关于较新版本JQUERY的Load方法会为页面引用文件自动附加时间戳参数阻止它缓存的解决方案.
- 1045 access denied for user 'root'@'localhost' using password yes 解决方案
- ubuntu16.4 安装离线mysql5.7.16
- XHTML