zoukankan      html  css  js  c++  java
  • poj1717 Dominoes (背包)

    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

    题目大意:给成一组多米诺牌,每个多米诺牌由上面和下面两组数组成,现要求可以翻动
    颠倒上下,使得多米诺上边的点数和减去下边的点数和的绝对值最小。

    题解:dp,背包,翻转或者不翻转,然后f[i][j],j表示反转后差为j的最小次数。

     1 #include<iostream>
     2 #include<cstring>
     3 #include<cstdio>
     4 #include<algorithm>
     5 #include<cmath>
     6 #define ll long long 
     7 #define inf 1000000007
     8 
     9 using namespace std;
    10 
    11 int n;
    12 int a[1005][2];
    13 int f[1005][10005];
    14 
    15 int main()
    16 {
    17     scanf("%d",&n);
    18     for(int i=1;i<=n;i++)
    19         scanf("%d%d",&a[i][0],&a[i][1]);
    20     memset(f,127,sizeof(f));
    21     f[0][5000]=0;
    22     for(int i=0;i<n;i++)
    23         for(int j=0;j<=10000;j++)
    24             if(f[i][j]<inf)
    25             {
    26                 int x1=a[i+1][0],x2=a[i+1][1];
    27                 f[i+1][j+x1-x2]=min(f[i][j],f[i+1][j+x1-x2]);
    28                 f[i+1][j+x2-x1]=min(f[i][j]+1,f[i+1][j+x2-x1]);
    29             }
    30     for(int i=0;i<=5000;i++)
    31         if(f[n][5000+i]<inf||f[n][5000-i]<inf)
    32         {
    33             printf("%d
    ",min(f[n][5000+i],f[n][5000-i]));
    34             break;
    35         }
    36 }
    
    
    
    
    
  • 相关阅读:
    框架面试题
    Mybatis的配置文件
    better-mybatis-generator逆向工程
    mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz的安装与配置
    apache-tomcat-8.5.40.tar.gz的安装与配置
    Linux系统的CentOS 7安装,Linux系统的登陆, VMware 12(32/64位)下载地址,VMware 15(32/64位)下载地址,安装VMware 12
    jdk-8u211-linux-x64.tar.gz的安装
    Javaweb的学习笔记(部分总结)
    蜂窝背景页面特效
    Linux操作系统ip的设置和vm快照
  • 原文地址:https://www.cnblogs.com/fengzhiyuan/p/7678262.html
Copyright © 2011-2022 走看看