poj 1717 Dominoes 背包



Dominoes
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 6464 Accepted: 2131

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




背包问题,数组的应用。
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long ll;
const int INF =0x3f3f3f3f;
const int maxn=1000    ;
int n,V,st;
int val[maxn+5];
int  dp[maxn+5][12010];

int fun(int x)
{
    return x+6000;
}
int ans,best;

void work()
{
    int x,y;
    V=st=0;
    for(int i=1;i<=n;i++)
    {
        scanf("%d%d",&x,&y);
        val[i]=x-y;
        st+=val[i];
        V+=abs(val[i]);
    }

    memset(dp[0],0x3f,sizeof dp[0]);
    dp[0][fun(st)]=0;
    for(int i=1;i<=n;i++)
    {

        int tval=-2*val[i];
        for(int v=-V;v<=V;v++)
        {
            dp[i][fun(v)]=dp[i-1][fun(v)];
            if( -V<=v-tval&&v-tval<=V)
            dp[i][fun(v)]=min(dp[i][fun(v)],dp[i-1][fun(v-tval)]+1);
        }
    }

    int best=INF,ans=INF;
    for(int v=-V;v<=V;v++) if(dp[n][fun(v)]!=INF)
    {
        if( abs(v)<best )
        {
            best=abs(v);
            ans=dp[n][fun(v)];
        }
        else if(abs(v)==best)
        {
            ans=min(ans,dp[n][fun(v)]);
        }
    }
    cout<<ans<<endl;




}
int main()
{
    while(~scanf("%d",&n) )
    work();

    return 0;
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值