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 ...
随机推荐
- 博客高亮代码及使用OpenLiveWriter修改之前博客
简述: 最近查阅前辈资料的时候,看到写的博客很有条理,回头看下自己的乱做麻花,然后来时研究: 他们的代码看起来很漂亮然后我就查资料,在网页版上一直没法出来像他们的格式,后查资料看来的使用客户端工具才 ...
- MIPS汇编程序设计——四则运算计算器
实验目的 运用简单的MIPS实现一个能够整数加减乘除的计算器,同时使自己更加熟悉这些指令吧 MIPS代码 #sample example 'a small calculater’ # data sec ...
- Web项目之Django基础
Django目录: python项目Django(web服务) python项目Django(HTTP协议) python项目Django(Django的安装与使用) python项目Django(U ...
- Java IO file文件的写入和读取及下载
一.FileWriter 和BufferedWriter 结合写入文件 FileWriter是字符流写入字符到文件.默认情况下,它会使用新的内容代替文件原有的所有内容,但是,当指定一个true值作为F ...
- CPP-基础:单目运算符重载
关于++运算符前置和后置重载的实现实例: #include <iostream> using namespace std; //创建时钟类 class Clock { public: Cl ...
- 在tomcat中配置连接池
在tomcat的conf/Catalina/localhost目录下配置项目路径,tomcat启动是会直接根据配置去加载项目. 虽然配置就一句话,但经常忘,今天记下来. 如果你的项目成名是:mypro ...
- What is the difference between try/except and assert?
assert only check if a condition is true or not and throw an exception. A try/except block can run a ...
- 学习笔记(_huaji_)
假如我没有见过太阳,我也许会忍受黑暗. 如果我知道自己会在哪里死去,我就永远都不去那儿.失败的经历,其实也有它的价值. 人的过失会带来错误,但要制造真正的灾难还得用计算机. 嘴角微微上扬已不复当年轻狂 ...
- 【Java_基础】Java的访问权限控制
1.类成员的访问权限控制 Java中类成员的访问权限分为四类:private,无(默认情况下),protected和public.其权限控制如下表所示: 修饰词 本类 同一个包的类 继承类 其他类 p ...
- systemverilog 之interface/timing region/program
1.connecting the testbench and the design 2.verilog connection review 3.systemverilog interfaces 4.s ...