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的最小次数。
 #include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cmath>
#define ll long long
#define inf 1000000007 using namespace std; int n;
int a[][];
int f[][]; int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++)
scanf("%d%d",&a[i][],&a[i][]);
memset(f,,sizeof(f));
f[][]=;
for(int i=;i<n;i++)
for(int j=;j<=;j++)
if(f[i][j]<inf)
{
int x1=a[i+][],x2=a[i+][];
f[i+][j+x1-x2]=min(f[i][j],f[i+][j+x1-x2]);
f[i+][j+x2-x1]=min(f[i][j]+,f[i+][j+x2-x1]);
}
for(int i=;i<=;i++)
if(f[n][+i]<inf||f[n][-i]<inf)
{
printf("%d\n",min(f[n][+i],f[n][-i]));
break;
}
}

												

最新文章

  1. 1.C语言中的数据类型
  2. 别不信!App三年内将被HTML5顶替彻底消失?
  3. EclEmma的介绍、安装与使用
  4. Linux root 密码重置与用户管理
  5. MFC和GDI+一起使用
  6. 【MySQL】查询使用临时表
  7. C# 笛卡尔积
  8. CMS 垃圾回收日志
  9. 常用 xwt 工具
  10. traceroute小结 come from CSDN author:houdong
  11. TreeView中右击直接获取节点的方法
  12. 实现JSON数据的存储和读取
  13. SDAU课程练习--problemO(1014)
  14. C#表达式目录树(Expression)
  15. linux,pthread(转)
  16. java 实现加密算法(在网上看的,保存)
  17. CODEFORCES ROUND #761 ANALYSES BY TEAM:RED &amp; BLACK
  18. Erlang-接口技术
  19. 利用QPainter绘制散点图
  20. Dockerfile 常用指令

热门文章

  1. SnowKiting
  2. JAVA的程序基本结构和数据类型
  3. 【春节版】年度精品 XP,32/64位Win7,32/64位Win8,32/64位Win10系统
  4. [习题]输入自己的生日(年/月/日)#2 -- 日历(Calendar)控件的时光跳跃,一次跳回五年、十年前?--TodaysDate属性、VisibleDate属性
  5. Python 学习日志9月18日
  6. 怎样将英文版的Eclipse转为中文版的?
  7. websocket 入门
  8. QT+动手设计一个登陆窗口+布局
  9. MVC使用方法
  10. js 两个数组对象根据账号比较去重,解决直接splice后数组索引改变