POJ1717 Dominoes[背包DP]
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 6731 | Accepted: 2234 |
Description

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
Source
题意:
多米诺骨牌有上下2个方块组成,每个方块中有1~6个点。现有排成行的
上方块中点数之和记为S1,下方块中点数之和记为S2,它们的差为|S1-S2|。例如在图8-1中,S1=6+1+1+1=9,S2=1+5+3+2=11,|S1-S2|=2。每个多米诺骨牌可以旋转180°,使得上下两个方块互换位置。
编程用最少的旋转次数使多米诺骨牌上下2行点数之差达到最小。
很普通的01背包
f[i][j+s]表示前i个差值为s的最少次数
滚动数组+for(int j=-i*6;j<=i*6;j++)轻松200ms快了两三倍
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int N=,INF=1e9;
inline int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x*f;
}
int n,w[N],s;
int f[][*N*];
void dp(){
memset(f,,sizeof(f));
f[][s]=;
for(int i=;i<=n;i++){
for(int j=-i*;j<=i*;j++){
int p=i&;
j+=s;
f[p][j]=INF;
if(j-w[i]>=) f[p][j]=min(f[p][j],f[p^][j-w[i]]);
if(j+w[i]>=&&f[p^][j+w[i]]<INF) f[p][j]=min(f[p][j],f[p^][j+w[i]]+);
j-=s;
}
}
}
int main(){
n=read();s=n*;
for(int i=;i<=n;i++) w[i]=read()-read();
dp();
int p=n&;
for(int d=;d<=n;d++){
if(f[p][s+d]!=INF){printf("%d",f[p][s+d]);break;}
if(f[p][s-d]!=INF){printf("%d",f[p][s-d]);break;}
}
}
POJ1717 Dominoes[背包DP]的更多相关文章
- poj1717 Dominoes (背包)
A domino is a flat, thumbsized tile, the face of which is divided into two squares, each left blank ...
- 背包dp整理
01背包 动态规划是一种高效的算法.在数学和计算机科学中,是一种将复杂问题的分成多个简单的小问题思想 ---- 分而治之.因此我们使用动态规划的时候,原问题必须是重叠的子问题.运用动态规划设计的算法比 ...
- hdu 5534 Partial Tree 背包DP
Partial Tree Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid= ...
- HDU 5501 The Highest Mark 背包dp
The Highest Mark Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?p ...
- Codeforces Codeforces Round #319 (Div. 2) B. Modulo Sum 背包dp
B. Modulo Sum Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/577/problem/ ...
- noj [1479] How many (01背包||DP||DFS)
http://ac.nbutoj.com/Problem/view.xhtml?id=1479 [1479] How many 时间限制: 1000 ms 内存限制: 65535 K 问题描述 The ...
- HDU 1011 树形背包(DP) Starship Troopers
题目链接: HDU 1011 树形背包(DP) Starship Troopers 题意: 地图中有一些房间, 每个房间有一定的bugs和得到brains的可能性值, 一个人带领m支军队从入口(房 ...
- BZOJ 1004: [HNOI2008]Cards( 置换群 + burnside引理 + 背包dp + 乘法逆元 )
题意保证了是一个置换群. 根据burnside引理, 答案为Σc(f) / (M+1). c(f)表示置换f的不动点数, 而题目限制了颜色的数量, 所以还得满足题目, 用背包dp来计算.dp(x,i, ...
- G - Surf Gym - 100819S -逆向背包DP
G - Surf Gym - 100819S 思路 :有点类似 逆向背包DP , 因为这些事件发生后是对后面的时间有影响. 所以,我们 进行逆向DP,具体 见代码实现. #include<bit ...
随机推荐
- windows对象模型分类
- 搭建Go开发及调试环境(LiteIDE + GoClipse) -- Windows篇
这里以Windows7 64位为例,如果是32位环境需安装对应版本程序. 一.安装golang1.2.2 1.3及1.3.1编译生成的二进制文件,无法使用LiteIDE23.2携带的gdb7.7进 ...
- 定制自己的mybatis生成
MyBatis Generator原生提供的生成方式targetRuntime有几种,但都不符合项目需求或想自定义自己的方法. 网上的文章也很多: 如:http://generator.sturgeo ...
- 【工业串口和网络软件通讯平台(SuperIO)教程】四.开发设备驱动
SuperIO相关资料下载:http://pan.baidu.com/s/1pJ7lZWf 1.1 开发准备 把“开发包”内的所有文件复制到项目的“bin”目录下,或项目下的专用生成目录.开发包 ...
- Entity Framework 中的Code First 中引入数据库函数
1,在项目中添加CodeFirstStoreFunctions包: Install-Package EntityFramework.CodeFirstStoreFunctions 2,注册注册函数,F ...
- OData V4 系列 .net应用
OData 学习目录 添加 OData Client Code Generator 扩展 添加OData T4生成工具 修改 T4 模板的 MetadataDocumentUri 运行Web项目,之后 ...
- WAMPServer安装和配置
1. 下载地址: www.wampserver.com www.php100.com 本机下载在 安装在 2. 自定义网站根目录 设置到这里 访问localhost就会访问到自定义的目录了假设 ...
- 如何面试前端工程师:GitHub 很重要
编者注:下面这篇文章从面试官的角度介绍到面试时可能会问到的一些问题. 我在Twitter和Stripe的一部分工作内容是面试前端工程师.其实关于面试你可能很有自己的一套,这里我想跟你们分享一下我常用的 ...
- mysql环境搭建
最近决定学习数据库,在比较了各个数据库之后,选择从mysql入手,主要原因: 开源 成熟,通用 用户量多,社区完善 入门简单 下载安装 mysql的官网下载地址:http://dev.mysql.co ...
- SAP RFC
什么是RFC? RFC是SAP系统和其他(SAP或非SAP)系统间的一个重要而常用的双向接口技术,也被视为SAP与外部通信的基本协议.简单地说,RFC过程就是系统调用当前系统外的程序模块,从而实现某个 ...