poj 1717==洛谷P1282 多米诺骨牌

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

CEOI 1997

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=1010;
int const inf=0x3f3f3f3f;
int a[N],b[N],n;
int f[N][N*6*2];//表示差值+p为j时的最小次数
int main(){
    memset(f,0x3f,sizeof(f));
    scanf("%d",&n);
    for(int i=1;i<=n;i++) scanf("%d%d",a+i,b+i);
    int v=n*6*2;
    int p=n*6;
    f[0][p]=0;
    //以下部分是两种不同的区间平移  其实第二种看起来更清晰 而且第一种会出现下标越界的情况 但是不知道为何竟然A了
    for(int i=1;i<=n;i++){
        for(int j=0;j<=v;j++){
            f[i][j]=min(f[i-1][j-(a[i]-b[i])],f[i-1][j-(b[i]-a[i])]+1);
        }
    }
    for(int i=1;i<=n;i++){
        for(int j=-p;j<=p;j++){
            f[i][j+p]=min(f[i-1][j+a[i]-b[i]+p],f[i-1][j+b[i]-a[i]+p]+1);
        }
    }
    //
    for(int i=0;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 0;
        }
    }
}

风格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[1010];
int f[1010][10100];
void first(){
    scanf("%d",&n);
    ref(i,1,n) scanf("%d%d",&a,&b),w[i]=a-b;
}
void go(){
    nn=5*n;
    memset(f,127,sizeof(f));
    f[1][w[1]+nn]=0;
    f[1][-w[1]+nn]=1;
    ref(i,2,n) def(j,10*n,0){
        if(j+w[i]>=0&&j+w[i]<=10*n)
          f[i][j]=min(f[i][j],f[i-1][j+w[i]]+1);
        if(j-w[i]>=0&&j-w[i]<=10*n)
          f[i][j]=min(f[i][j],f[i-1][j-w[i]]);
    }
    if(f[n][5*n]<Q) {printf("%d\n",f[n][5*n]);return;}
    else{
        for(int i=nn-1,j=nn+1;i>=1&&j<=2*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 0;
}

 
时间: 2024-08-26 12:36:07

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

洛谷 P1282 多米诺骨牌

题目描述 多米诺骨牌有上下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. 输入输出格式 输入格式: 输入文件的第一行是一个正

洛谷P1282 多米诺骨牌

动态规划 题意: 对多米诺骨牌进行翻转,使其上下值最接近,求最小的翻转次数 1.状态 dp[ i ][ j ] 表示上面那排前i个数 和为 j 所需要的最小的翻转次数 2.状态转移方程 dp[ i ][ j ] = min(dp[ i ][ j ],dp[ i-1 ][ j-a[ i ] ] ) ; dp[ i ][ j ] = min(dp[ i ][ j ],dp[ i-1 ][ j-b[ i ] ]+1 ) ; 3.初始值 dp[ i ][ j ] = inf dp[ 0 ][ 0 ] =

P1282 多米诺骨牌

P1282 多米诺骨牌 题目描述 多米诺骨牌有上下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. 输入输出格式 输入格式:

luogu P1282 多米诺骨牌

题目描述 多米诺骨牌有上下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. 输入输出格式 输入格式: 输入文件的第一行是一个正

洛谷P1725 琪露诺 单调队列优化 DP

洛谷P1725 琪露诺 单调队列优化 DP 题意:1--n 每个点都有一个权值,从当前点i可以到达i+l--i+r 之间的点, 动态规划 方程 为 f[ i ] = max(f[ i ],f[ k ] ) +a[ i ] i-r<=k<=i-l 然而这样复杂度 就为 n^2 因为相当于 dp 是在求 一段区间的最大值,而这个最大值就可以用O(n) 来维护 注意 这个O(n) 是均摊O(n) 即将所有固定区间长度的 最大值求出来 是 O(n)的这样就把复杂度降到 O(n) 级别了 1 #incl

POJ 1135 Domino Effect(最短路 多米诺骨牌)

题意 题目描述: 你知道多米诺骨牌除了用来玩多米诺骨牌游戏外,还有其他用途吗?多米诺骨牌游戏:取一 些多米诺骨牌,竖着排成连续的一行,两张骨牌之间只有很短的空隙.如果排列得很好,当你推 倒第 1张骨牌,会使其他骨牌连续地倒下(这就是短语"多米诺效应"的由来). 然而当骨牌数量很少时,这种玩法就没多大意思了,所以一些人在 80 年代早期开创了另一个 极端的多米诺骨牌游戏:用上百万张不同颜色.不同材料的骨牌拼成一幅复杂的图案.他们开创 了一种流行的艺术.在这种骨牌游戏中,通常有多行骨牌同时

状态压缩动态规划 -- 多米诺骨牌

用1*2 的骨牌通过组合拼成 m * n 的大矩形,问有几种拼法. 题目链接:http://poj.org/problem?id=2411 状态转移: 1.由于上一行的该列竖直放置骨牌为 0,影响到当前行的该列,当前行的该列为 1 2.当前行骨牌横放,上一行骨牌横放, 都为11 3.上一行该列置为 1,当前行当前列立着放为 0 #include <iostream> #include <cstring> using namespace std; #define MAXSIZE 12

FZU 2163 多米诺骨牌

Problem Description Vasya很喜欢排多米诺骨牌.他已经厌倦了普通的多米诺骨牌,所以他用不同高度的多米诺骨牌.他从左边到右边,把n个多米诺骨牌沿一个轴放在桌子上.每一个多米诺骨牌垂直于该轴,使该轴穿过其底部的中心.第i个多米诺骨牌具有坐标xi与高度hi.现在Vasya想要知道,对于每一个多米诺骨牌如果他推倒的话,右侧会有多少个多米诺骨牌也会倒下. 想想看,一个多米诺倒下,如果它严格的触动右侧的多米诺骨牌,被触碰的也会倒下.换句话说,如果多米诺骨牌(初始坐标x和高度h)倒下,会

S6 edge+的多米诺骨牌效应:大屏的趋势

日前,为庆祝三星S6 edge+国行版的顺利发售,三星盖乐世社区的一些粉丝自发组织了三星疯狂"盖星人"第一期活动--活动现场除了可以对S6 edge+进行全方面体验之外,还将演示著名的多米诺骨牌游戏.作为一个对手机产品有着持续关注和体验热情的"用户"来说,这样的活动自然不能错过,于是在通过三星盖乐世社区的规则报名之后,笔者顺利通过审核,之后终于迎来了到现场体验的日期. 这次的疯狂"盖星人"线下活动主要有两个内容环节,分别是三星S6 edge+的产