Dominoes
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 6731   Accepted: 2234

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.

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.

Output

Output the smallest number of turns needed to minimise the gap between the top line and the bottom line.

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]的更多相关文章

  1. poj1717 Dominoes (背包)

    A domino is a flat, thumbsized tile, the face of which is divided into two squares, each left blank ...

  2. 背包dp整理

    01背包 动态规划是一种高效的算法.在数学和计算机科学中,是一种将复杂问题的分成多个简单的小问题思想 ---- 分而治之.因此我们使用动态规划的时候,原问题必须是重叠的子问题.运用动态规划设计的算法比 ...

  3. hdu 5534 Partial Tree 背包DP

    Partial Tree Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid= ...

  4. 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 ...

  5. 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/ ...

  6. noj [1479] How many (01背包||DP||DFS)

    http://ac.nbutoj.com/Problem/view.xhtml?id=1479 [1479] How many 时间限制: 1000 ms 内存限制: 65535 K 问题描述 The ...

  7. HDU 1011 树形背包(DP) Starship Troopers

    题目链接:  HDU 1011 树形背包(DP) Starship Troopers 题意:  地图中有一些房间, 每个房间有一定的bugs和得到brains的可能性值, 一个人带领m支军队从入口(房 ...

  8. BZOJ 1004: [HNOI2008]Cards( 置换群 + burnside引理 + 背包dp + 乘法逆元 )

    题意保证了是一个置换群. 根据burnside引理, 答案为Σc(f) / (M+1). c(f)表示置换f的不动点数, 而题目限制了颜色的数量, 所以还得满足题目, 用背包dp来计算.dp(x,i, ...

  9. G - Surf Gym - 100819S -逆向背包DP

    G - Surf Gym - 100819S 思路 :有点类似 逆向背包DP , 因为这些事件发生后是对后面的时间有影响. 所以,我们 进行逆向DP,具体 见代码实现. #include<bit ...

随机推荐

  1. 在Hibernate框架中详谈一级缓存

    在学习Hibernate的过程中我们肯定会碰上一个名词---缓存,一直都听说缓存机制是Hibernate中的一个难点,它分为好几种,有一级缓存,二级缓存和查询缓存 今天呢,我就跟大家分享分享我所理解的 ...

  2. Win10 IoT C#开发 4 - UART 串口通信

    Windows 10 IoT Core 是微软针对物联网市场的一个重要产品,既可以开发设备UI与用户交互式操作,又可以控制GPIO等接口,使得原来嵌入式繁琐的开发变得简单.通过Remote Debug ...

  3. 生成树形结构的json字符串代码(c#)供前端angular tree使用.

    框架是使用EF6.0.可以针对返回的值使用Newtonsoft.Json.dll(百度搜一下)来对返回的值序列化为json字符串,如果对以下值那就是使用JsonConvert.SerializeObj ...

  4. de4dot3.14更新文件打包下载

    刚发现de4dot更新了,虽然只是10月份的文件更新,并未发布新的release,但好多人还不会编译... 关于de4dot有何功能就不再讲了. 本文主要提供编译通过后的打包文件下载. 首先下载de4 ...

  5. powershell脚本,命令行参数传值,并绑定变量的例子

    这是小技巧文章,所以文章不长.但原创唯一,非常重要.我搜了下,还真没有人发 powershell怎样 [命令行 参数 绑定],所以我决定写成博客. 搜索关键字如下: powershell 命令行 参数 ...

  6. Linux 定时任务crontab

    crontab定时任务格式 1 * * * * * command 2 第1列表示分钟1-59 每分钟用*或者 */1表示 3 第2列表示小时1-23(0表示0点) 4 第3列表示日期1-31 5 第 ...

  7. centos svn服务器搭建

    1.安装svnyum install subversion查看安装目录rpm -ql subversion 查看yum安装subversion的位置 2.创建仓库创建版本库目录mkdir -p /va ...

  8. 帆布指纹识别(canvas fingerprinting)

    广告联盟或许网站运营者都希望能够精准定位并标识每一个个体,通过对用户行为的分析(浏览了哪些页面?搜索了哪些关键字?对什么感兴趣?点了哪些按钮?用了哪些功能?看了哪些商品?把哪些放入了购物车等等),为用 ...

  9. angular源码分析:$compile服务——指令的编写

    这一期中,我不会分析源码,只是翻译一下"https://docs.angularjs.org/api/ng/service/$compile",当然不是逐字逐句翻译,讲解指令应该如 ...

  10. 商业智能SAAS走向中小企业

    20多年前,Gartner提出了商业智能的概念,并将其定义为“一类由数据仓库.查询报表.数据分析.数据挖掘等部分组成的,以帮助企业决策的技术及应用”.从技术上讲,商业智能是数据仓库.OLAP和数据挖掘 ...