poj1717 Dominoes (背包)
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 题目大意:给成一组多米诺牌,每个多米诺牌由上面和下面两组数组成,现要求可以翻动
颠倒上下,使得多米诺上边的点数和减去下边的点数和的绝对值最小。 题解:dp,背包,翻转或者不翻转,然后f[i][j],j表示反转后差为j的最小次数。
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cmath>
#define ll long long
#define inf 1000000007 using namespace std; int n;
int a[][];
int f[][]; int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++)
scanf("%d%d",&a[i][],&a[i][]);
memset(f,,sizeof(f));
f[][]=;
for(int i=;i<n;i++)
for(int j=;j<=;j++)
if(f[i][j]<inf)
{
int x1=a[i+][],x2=a[i+][];
f[i+][j+x1-x2]=min(f[i][j],f[i+][j+x1-x2]);
f[i+][j+x2-x1]=min(f[i][j]+,f[i+][j+x2-x1]);
}
for(int i=;i<=;i++)
if(f[n][+i]<inf||f[n][-i]<inf)
{
printf("%d\n",min(f[n][+i],f[n][-i]));
break;
}
}
poj1717 Dominoes (背包)的更多相关文章
- POJ1717 Dominoes[背包DP]
Dominoes Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6731 Accepted: 2234 Descript ...
- 【USACO 3.1】Stamps (完全背包)
题意:给你n种价值不同的邮票,最大的不超过10000元,一次最多贴k张,求1到多少都能被表示出来?n≤50,k≤200. 题解:dp[i]表示i元最少可以用几张邮票表示,那么对于价值a的邮票,可以推出 ...
- HDU 3535 AreYouBusy (混合背包)
题意:给你n组物品和自己有的价值s,每组有l个物品和有一种类型: 0:此组中最少选择一个 1:此组中最多选择一个 2:此组随便选 每种物品有两个值:是需要价值ci,可获得乐趣gi 问在满足条件的情况下 ...
- HDU2159 二维完全背包
FATE Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- CF2.D 并查集+背包
D. Arpa's weak amphitheater and Mehrdad's valuable Hoses time limit per test 1 second memory limit p ...
- UVALive 4870 Roller Coaster --01背包
题意:过山车有n个区域,一个人有两个值F,D,在每个区域有两种选择: 1.睁眼: F += f[i], D += d[i] 2.闭眼: F = F , D -= K 问在D小于等于一定限度的时 ...
- 洛谷P1782 旅行商的背包[多重背包]
题目描述 小S坚信任何问题都可以在多项式时间内解决,于是他准备亲自去当一回旅行商.在出发之前,他购进了一些物品.这些物品共有n种,第i种体积为Vi,价值为Wi,共有Di件.他的背包体积是C.怎样装才能 ...
- HDU3466 Proud Merchants[背包DP 条件限制]
Proud Merchants Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others) ...
- POJ1112 Team Them Up![二分图染色 补图 01背包]
Team Them Up! Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 7608 Accepted: 2041 S ...
随机推荐
- js3
举几个小例子: 1. 九九乘法表 var s = "<table>"; for (var i=1;i<=9;i++) { s += "<tr> ...
- hihoCoder #1080 : 更为复杂的买卖房屋姿势 (线段树,多tag)
题意: 有编号为0~n的n+1个房屋,给出每个房屋的起始价格,随后给出m种修改,每次修改都要进行输出所有房屋的价格总和.修改有两种方式:(1)政府调控,编号L~R全置为同一价格(0)房屋自行涨跌,编号 ...
- codevs 1131 统计单词数 2011年NOIP全国联赛普及组
时间限制: 1 s 空间限制: 128000 KB 题目等级 : 白银 Silver 题目描述 Description 一般的文本编辑器都有查找单词的功能,该功能可以快速定位特定单词在文章中的位 ...
- mysql Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nona
1. 操作mysql的时候提示如下错误 [Err] 1055 - Expression #1 of ORDER BY clause is not in GROUP BY clause and cont ...
- Python 元组、字典、集合操作总结
元组 a=('a',) a=('a','b') 特点 有序 不可变,不可以修改元组的值,无法为元组增加或者删除元素 元组的创建 a=('a',) a=('a','b') tuple('abcd') 转 ...
- 更新Svn客户端后,右键菜单中没有TortoiseSVN
环境: OS: Windows XP sp3 升级后SVNServer: VisualSVN Server 2.7.3 升级后SVNClient: 小乌龟: ...
- mask rcnn和roi-align
faster-rcnn的github源码中是round四舍五入 但kaiming he的ppt是直接取整 1.讲roi-align和roi-pooling区别并且详细阐述roi-align过程的博客: ...
- WINDOWS-API:API函数大全
操作系统除了协调应用程序的执行.内存分配.系统资源管理外,同时也是一个很大的服务中心,调用这个服务中心的各种服务(每一种服务是一个函数),可以帮肋应用程序达到开启视窗.描绘图形.使用周边设备的目的,由 ...
- snprintf()返回值的陷阱
int snprintf(char *restrict buf, size_t n, const char * restrict format, ...); 函数说明:最多从源串中拷贝n-1个字符到 ...
- 歌乐第二弹:C++九九八十一
第一弹传送门:极乐净土 二话不说,上代码(注意事项在第一弹里): #include <windows.h> //q前缀为低音,g为高音,s前缀为半音阶 const int q1 = 131 ...