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. Oracle取消用户连续登录失败次数限制

    当用户连续登录失败次数过多时,Oracle会锁定该用户,“FAILED_LOGIN_ATTEMPTS”用于设置最大次数,超过该值则锁定该帐号. 要取消用户连续登录失败次数的限制可以按照以下方法操作: ...

  2. fastq-dump 报错 解决方案

    命令行: ~/tools/sratoolkit/sratoolkit.2.3.2-5-centos_linux64/bin/fastq-dump --split-spot --gzip rhesus_ ...

  3. 【工匠大道】markdown使用技巧

    本文地址 提纲: 1. 概述 2. 常见技巧 3. 参考文档 1. 概述 常见的markdown的技巧,这里不再谈了,主要是自己感觉比较少见但有用的技巧. 2. 常见技巧   1)[空格]生成空格的效 ...

  4. 【工匠大道】将项目同时托管到Github和Git@OSC

    原文地址 摘要: Github是最大的git代码托管平台,​GIT@OSC是国内最大的git代码托管平台,支持免费私有库,支持SVN操作,用户众多.很多用户需要同时将代码托管到两个平台,这篇文章的主要 ...

  5. JavaScript变换表格边框颜色

    效果查看:http://hovertree.com/texiao/js/2.htm 代码如下,保存到HTML文件也可以查看效果: <html> <head> <meta ...

  6. jquery点击切换背景色

    刚在自己的前端技术群里有个小伙伴儿问到这个问题,点击的时候进行红色背景和白色背景的切换,然后我就临时想到了一个解决方法,在这里记录一下吧,希望还有别的刚学习前端的小伙伴儿能用到,大神就请绕道咯!另外提 ...

  7. XSS跨站测试代码大全

    '><script>alert(document.cookie)</script>='><script>alert(document.cookie)&l ...

  8. GeoEvent使用问题及解决方法整理

    假如GeoEvent的部署环境是一个典型的WebGIS架构(Portal+GIS Server),往往会遇到一些问题,例如: 问题:发布的StreamService流服务无法查看. 原因:默认发布的S ...

  9. 从头开始构建LINUX [LFS 脚本]

    脚本共享在这 http://pan.baidu.com/s/1nt6yiH7 version-check.sh : 这个是检查HOST机器的软件依赖情况 host-dep.sh:针对ubuntu10_ ...

  10. Android开发学习——打电话应用

    打电话应用 system/app/phone.apk  这个是打电话应用,这个Java API 不允许应用级程序员改写,系统级才可以 system/app/dialer.apk  这个是拨号器应用,可 ...