Dominoes
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 6571   Accepted: 2178

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

P1282 多米诺骨牌

  • 题目提供者该用户不存在
  • 标签动态规划
  • 难度提高+/省选-
  • 通过/提交316/1095

提交该题 讨论 题解 记录

题目描述

多米诺骨牌有上下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行点数之差达到最小。

对于图中的例子,只要将最后一个多米诺骨牌旋转180°,可使上下2行点数之差为0。

输入输出格式

输入格式:

输入文件的第一行是一个正整数n(1≤n≤1000),表示多米诺骨牌数。接下来的n行表示n个多米诺骨牌的点数。每行有两个用空格隔开的正整数,表示多米诺骨牌上下方块中的点数a和b,且1≤a,b≤6。

输出格式:

输出文件仅一行,包含一个整数。表示求得的最小旋转次数。

输入输出样例

输入样例#1:

4
6 1
1 5
1 3
1 2
输出样例#1:

1

风格1:
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
int const N=;
int const inf=0x3f3f3f3f;
int a[N],b[N],n;
int f[N][N**];//表示差值+p为j时的最小次数
int main(){
memset(f,0x3f,sizeof(f));
scanf("%d",&n);
for(int i=;i<=n;i++) scanf("%d%d",a+i,b+i);
int v=n**;
int p=n*;
f[][p]=;
//以下部分是两种不同的区间平移 其实第二种看起来更清晰 而且第一种会出现下标越界的情况 但是不知道为何竟然A了
for(int i=;i<=n;i++){
for(int j=;j<=v;j++){
f[i][j]=min(f[i-][j-(a[i]-b[i])],f[i-][j-(b[i]-a[i])]+);
}
}
for(int i=;i<=n;i++){
for(int j=-p;j<=p;j++){
f[i][j+p]=min(f[i-][j+a[i]-b[i]+p],f[i-][j+b[i]-a[i]+p]+);
}
}
//
for(int i=;i<=p;i++){
if(min(f[n][p+i],f[n][p-i])!=inf){
printf("%d\n",min(f[n][p+i],f[n][p-i]));
return ;
}
}
}

风格2:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define ref(i,x,y)for(register int i=x;i<=y;i++)
#define def(i,x,y)for(register int i=x;i>=y;i--)
#define Q 2000
int a,b,n,nn,w[];
int f[][];
void first(){
scanf("%d",&n);
ref(i,,n) scanf("%d%d",&a,&b),w[i]=a-b;
}
void go(){
nn=*n;
memset(f,,sizeof(f));
f[][w[]+nn]=;
f[][-w[]+nn]=;
ref(i,,n) def(j,*n,){
if(j+w[i]>=&&j+w[i]<=*n)
f[i][j]=min(f[i][j],f[i-][j+w[i]]+);
if(j-w[i]>=&&j-w[i]<=*n)
f[i][j]=min(f[i][j],f[i-][j-w[i]]);
}
if(f[n][*n]<Q) {printf("%d\n",f[n][*n]);return;}
else{
for(int i=nn-,j=nn+;i>=&&j<=*nn;i--,j++){
if(f[n][i]<Q||f[n][j]<Q){
printf("%d\n",min(f[n][i],f[n][j]));
return;
}
}
}
}
int main(){
first();
go();
return ;
}

 

poj 1717==洛谷P1282 多米诺骨牌的更多相关文章

  1. 洛谷P1282 多米诺骨牌 (DP)

    洛谷P1282 多米诺骨牌 题目描述 多米诺骨牌有上下2个方块组成,每个方块中有1~6个点.现有排成行的 上方块中点数之和记为S1,下方块中点数之和记为S2,它们的差为|S1-S2|.例如在图8-1中 ...

  2. 洛谷P1282 多米诺骨牌

    P1282 多米诺骨牌 题目描述 多米诺骨牌有上下2个方块组成,每个方块中有1~6个点.现有排成行的 上方块中点数之和记为S1,下方块中点数之和记为S2,它们的差为|S1-S2|.例如在图8-1中,S ...

  3. 【01背包】洛谷P1282多米诺骨牌

    题目描述 多米诺骨牌有上下2个方块组成,每个方块中有1~6个点.现有排成行的 上方块中点数之和记为S1,下方块中点数之和记为S2,它们的差为|S1-S2|.例如在图8-1中,S1=6+1+1+1=9, ...

  4. 洛谷 P1282 多米诺骨牌 ( 线性DP )

    题意 : 题目链接 分析 :  一开始这个想法也有想到,但是貌似要开很大数组,就感觉应该不行 遂放弃想其他方法,万万没想到注意到可以滚动优化(其实不优化也可以过) 定义 dp[i][j] 表示 到第 ...

  5. 洛谷P1282 多米诺骨牌【线性dp】

    题目:https://www.luogu.org/problemnew/show/P1282 题意: 给定n个牌,每个牌有一个上点数和下点数.可以通过旋转改变交换上下点数. 问使得上点数之和和下点数之 ...

  6. 洛谷 [P1282] 多米诺骨牌

    这道题是一道背包问题,考虑一个背包, 显然如果我们直接设dp[i]表示前i个使差值最小所需的最少翻转次数,是具有后效性的. 所以我们将直接求最值,改为求某个值是否可行,这种求最值转变为求可行性的思想是 ...

  7. 洛谷 P1282 多米诺骨牌("01"背包)

    传送门 https://www.cnblogs.com/violet-acmer/p/9852294.html 参考资料: [1]:https://blog.csdn.net/Darost/artic ...

  8. yzoj P2043 & 洛谷 P1282 多米诺骨牌 题解

    题意 类似于就是背包. 解析 代码 跟解析有点不一样v[i]价值,w[i]重量,s背包容积,背包转移即可. #include<bits/stdc++.h> using namespace ...

  9. P1282 多米诺骨牌【dp】

    P1282 多米诺骨牌 提交 20.02k 通过 6.30k 时间限制 1.00s 内存限制 125.00MB 题目提供者洛谷 难度提高+/省选- 历史分数100 提交记录 查看题解 标签   查看算 ...

随机推荐

  1. 【Java】Java_09 类型转换

    1.自动类型转换 自动类型转换:容量小的数据类型可以自动转换为容量大的数据类型.在图中,黑色的实线表示无数据丢失的自动类型转换,而红色的虚线表示在转换时可能会精度的损失. 特例: 可以将整型常量直接赋 ...

  2. Photoshop | 快速抠头发(调整边缘/选择并遮住)

    ———————————————————————————————————————————— Photoshop快速抠头发(CS6中调整边缘功能,CC2017中更名为选择并遮住) - - - - - - ...

  3. iOS学习笔记-自定义过渡动画

    代码地址如下:http://www.demodashi.com/demo/11678.html 这篇笔记翻译自raywenderlick网站的过渡动画的一篇文章,原文用的swift,由于考虑到swif ...

  4. GIS中的坐标系

    原文地址:http://www.cnblogs.com/onsummer/p/7451128.html 从第一次上地图学的课开始,对GIS最基本的地图坐标系统就很迷.也难怪,我那时候并不是GIS专业的 ...

  5. HDU 3085 双广

    n*m地图上有 '. ':路 'X':墙 'Z':鬼,每秒蔓延2个单位长度,能够穿墙.共两个,每秒開始时鬼先动 'M':一号,每分钟可移动3个单位长度 'G':二号,每分钟课移动1个单位长度 问两人能 ...

  6. systemd 管理python 程序

    [Unit] Description = test-minapp After = network.target [Service] PermissionsStartOnly = true PIDFil ...

  7. SQL 子查询 EXISTS 和 NOT EXISTS

    MySQL EXISTS 和 NOT EXISTS 子查询语法如下: SELECT … FROM table WHERE EXISTS (subquery) 该语法可以理解为:将主查询的数据,放到子查 ...

  8. testng入门_单元测试

    1.定义TestNG 的配置文件 <test name="exampletest1">        <classes> <!--1.只执行com.t ...

  9. ssh-keygen配合ssh_config免密码登录VPS

    ssh-keygen配合ssh_config免密码登录VPS Posted by fiture / 2012年12月29日 / 「Ubuntu」「分享」 用过终端登录远程服务器或者VPS的童鞋都用过类 ...

  10. Lua顺序 执行顺序

    1.4.2. Lua顺序 Nginx下Lua处理阶段与使用范围: init_by_lua http set_by_lua server, server if, location, location i ...