uva 562 Dividing coins

Dividing coins

It‘s commonly known that the Dutch have invented copper-wire. Two Dutch men were fighting over a nickel, which was made of copper. They were both so eager to get it and the fighting was so fierce, they stretched the coin to great length and thus created
copper-wire.

Not commonly known is that the fighting started, after the two Dutch tried to divide a bag with coins between the two of them. The contents of the bag appeared not to be equally divisible. The Dutch of the past couldn‘t stand the fact that a division should
favour one of them and they always wanted a fair share to the very last cent. Nowadays fighting over a single cent will not be seen anymore, but being capable of making an equal division as fair as possible is something that will remain important forever...

That‘s what this whole problem is about. Not everyone is capable of seeing instantly what‘s the most fair division of a bag of coins between two persons. Your help is asked to solve this problem.

Given a bag with a maximum of 100 coins, determine the most fair division between two persons. This means that the difference between the amount each person obtains should be minimised. The value of a coin varies from 1 cent to 500 cents. It‘s not allowed to
split a single coin.

Input

A line with the number of problems n, followed by n times:

  • a line with a non negative integer m () indicating the number of coins in the bag
  • a line with m numbers separated by one space, each number indicates the value of a coin.

Output

The output consists of n lines. Each line contains the minimal positive difference between the amount the two persons obtain when they divide the coins from the corresponding bag.

Sample Input

2
3
2 3 5
4
1 2 4 6

Sample Output

0
1

Miguel A. Revilla

1998-03-10

题意:平均分成两份,使两份尽量公平,求差值。

思路:01背包,容量设成sum/2。dp[i][j]=max(dp[i-1][j],dp[i-1][j-v[i]]+v[i]).

AC代码:

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <stdlib.h>

using namespace std;

int v[105];
int dp[105][50005];

int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        int m;
        scanf("%d",&m);
        int sum=0;
        memset(v,0,sizeof(v));
        for(int i=1;i<=m;i++){
            scanf("%d",&v[i]);
            sum+=v[i];
            //printf("%d\n",v[i]);
        }
        sort(v,v+m);
        //for(int i=0;i<m;i++){
        //    printf("%d\n",&v[i]);
        //}
        int n;
        n=sum/2;
        memset(dp,0,sizeof(dp));
        for(int i=1;i<=m;i++){
            for(int j=0;j<=n;j++){
                dp[i][j]=dp[i-1][j];
                if(j-v[i]>=0)
                    dp[i][j]=max(dp[i][j],dp[i-1][j-v[i]]+v[i]);
            }
        }
        int a=dp[m][n];
        int b=sum-dp[m][n];
        int ans=abs(a-b);
        printf("%d\n",ans);
    }
    return 0;
}



uva 562 Dividing coins,布布扣,bubuko.com

时间: 07-26

uva 562 Dividing coins的相关文章

UVA 562 Dividing coins --01背包的变形

01背包的变形. 先算出硬币面值的总和,然后此题变成求背包容量为V=sum/2时,能装的最多的硬币,然后将剩余的面值和它相减取一个绝对值就是最小的差值. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; #define N 50007 int c[102],d

UVA 562 Dividing coins (01背包基础)

[题目链接]:click here~~ 代码: /* * Problem: UVA No.562 * Running time: 0MS * Complier: C++ * Author: ACM_herongwei * Create Time: 11:12 2015/9/9 星期三 * zeroonebags * 将金币总价值的一半作为背包容量,然后zeronebags */ #include <stdio.h> #include <iostream> #include <

(背包dp)UVA - 562 Dividing coins

题意:有n个硬币,每个硬币有个价值,两个人分配硬币,要求最公平分配时候两人拿到的钱的差. 分析:很明显,两人拿到的钱的差越小越公平. 一开始想,一定是一人一半最公平,所以直接把总和sum/2,对着half跑01背包,但是WA了,修改后分别讨论奇偶,额外进行一次sum-half的01背包,也WA,仔细想想觉得有些漏洞. 所以,这题其实可以干脆直接跑sum的背包,不断更新ans=min(ans,sum-dp[j]*2)就行了.如果ans==inf,表示不能分,也就是1个,这时输出0. 代码: 1 #

UVA 562 Dividing coins (01背包)

//平分硬币问题 //对sum/2进行01背包,sum-2*dp[sum/2] #include <iostream> #include <cstring> #include <algorithm> using namespace std; int value[100000],dp[100000]; int main() { int n,m,sum,sum1; cin>>n; while(n--) { cin>>m; sum=0; for(int

UVA 562 Dividing coins 分硬币(01背包,简单变形)

题意:一袋硬币两人分,要么公平分,要么不公平,如果能公平分,输出0,否则输出分成两半的最小差距. 思路:将提供的整袋钱的总价取一半来进行01背包,如果能分出出来,就是最佳分法.否则背包容量为一半总价的包能装下的硬币总值就是其中一个人能分得的最多的钱了,总余下的钱减去这包硬币总值.(只需要稍微考虑一下总值是奇数/偶数的问题) 1 #include <iostream> 2 #include <stdio.h> 3 #include <string.h> 4 #includ

uva562 - Dividing coins(01背包)

题目:uva562 - Dividing coins(01背包) 题目大意:给出N个硬币,每个硬币有对应的面值.要求将这些硬币分成两部分,求这两部分最小的差值. 解题思路:先求这些硬币能够凑出的钱(0, 1背包),然后再从sum(这些硬币的总和)/2开始往下找这个值能否由这些硬币凑出.要注意的是,可以由前n个硬币组成那样也是可以组成的面值. 代码: #include <cstdio> #include <cstring> const int N = 105; const int m

UVALive5583 UVA562 Dividing coins

Regionals 1996 >> Europe - Northwestern 问题链接:UVALive5583 UVA562 Dividing coins. 题意简述:输入测试用例数n,每个测试用例包括金币的数量m和m个正整数是金币的币值.将这些金币分为两堆,使得其差值最小,求最小的差值. 问题分析:可以用0/1背包的动态规划方法来解决这个问题.将背包的容量设为所有金币币值之和的一半,将这一堆金币尽可能多地放入背包即可.再计算一下差值即可. 0/1背包动态规划法的递归式为f(j,X),其中j

Dividing coins (01背包)

It's commonly known that the Dutch have invented copper-wire. Two Dutch men were fighting over a nickel, which was made of copper. They were both so eager to get it and the fighting was so fierce, they stretched the coin to great length and thus crea

UVa 711 - Dividing up

题目:有一些石头,每块石头有一个价值价值(1~6中的一个整数),现在把石头分成两组, 问能否分成价值和相同的两组. 分析:dp,01背包.背包容量为总和的一半,判断能否放满即可. 说明:数据较大,使用二进制拆分╮(╯▽╰)╭. #include <iostream> #include <cstring> #include <cstdio> using namespace std; int f[60001],c[6],w[6] = {1,2,3,4,5,6}; int p