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 ($m le 100$) 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

袋子里有一些不同面值的硬币,把它们分给两个人,为了尽量达到公平,问两个人得到的钱的差最小是多少

我最初的想法是一个贪心的思路,设diff表示两个人的钱数之差,对每一枚硬币,diff=fabs(diff-coin[i]),也就是在分硬币的时候始终保持两个之间的差值最小,但后来发现了一组测试数据表明了这个思路是不正确的:

1

3

8 7 5 4 2

后来改用DP的方法,设dp[i]数组,若可以用一些硬币拼出i这么多钱,则dp[i]=true,反之dp[i]=false,这样最后之要从sum/2向两侧找到最近的dp[x]=true即可。(sum为所有硬币之和)

状态转移方程为:dp[i]=dp[i-coin[j]]

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 
 5 using namespace std;
 6 
 7 int m,sum;
 8 int coin[105];
 9 bool dp[50550];
10 
11 int main()
12 {
13     int kase;
14 
15     scanf("%d",&kase);
16 
17     while(kase--)
18     {
19         scanf("%d",&m);
20 
21         sum=0;
22         memset(dp,false,sizeof(dp));
23         dp[0]=true;
24 
25         for(int i=0;i<m;i++)
26         {
27             scanf("%d",&coin[i]);
28             sum+=coin[i];
29             // 注意这个for循环一定要是倒着进行的
30             for(int j=50000;j>=coin[i];j--)
31                 if(dp[j-coin[i]])
32                     dp[j]=true;
33         }
34 
35         for(int i=sum/2;i>=0;i--)
36             if(dp[i])
37             {
38                 printf("%d
",sum-2*i);
39                 break;
40             }
41     }
42 
43     return 0;
44 }
[C++]
原文地址:https://www.cnblogs.com/lzj-0218/p/3565968.html