Uva 562-Dividing coins

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=503

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

I have not been able to come up with a suitable solution. Could someone suggest some answer for this problem?

This is a variation of the knapsack problem (Subset sum). A common version of subset sum problem is:
“Given n items with non-negative integer weights w(i) and an integer W, determine a subset whose total weight is maximized and is less than than W.”

Here you have to find a subset of coins whose sum is nearest to T/2, where T is the sum of values of all the coins.
So, you just need to set the value of W in the subset sum problem as T/2. Suppose, the total weight of the set returned is A, then 2*(T/2-A) is the answer.

Now, let’s take a look at the dynamic programming solution of the subset sum problem.
Let the items be numbered from 1 to n.

We define M(i,w): The maximum weight that can be filled using items {1,…,i} subject to the sum being <= W.
Now we need to write a recurrence relation for computing M(i,w) using smaller subprolems.

Case 1: i’th item is not in the optimal solution.

M(i,w) = M(i-1,w)

Case 2: i’th item is in the optimal solution.

M(i,w) = M(i-1,w-w(i))+ w(i)

Therefore the dynamic programming solution is:

  • If w(i) > W, then M(i,w) = M(i-1,w)
  • If w(i) <= W, then M(i,w) = max(M(i-1,w), M(i-1,w-w(i)) + w(i))
  • For all w<=W, M(1,w) = w(1) if w(1) <= w, 0 otherwise.

Time complexity is O(nW).
In the given problem, n<=100, and W<=25000 (500 * 100/2), therefore nW <= 2.5*(10^6).

P.S. I haven’t solved this problem on UVa Online judge, but I have faced a similar problem somewhere else.

Reference: http://www.cse.iitd.ac.in/~rjaiswal/2014/csl356/Notes/Week-08/lec-1.pdf

2 Likes

Thanks a lot.:slight_smile:

Here is the C++ implementation for the same.
#include<bits/stdc++.h>
using namespace std;

int main()
{
int t;
cin>>t;
while(t–)
{
long long n;
cin>>n;
long long arr[n];
long long sum=0;
for(int i=0;i<n;i++)
{
cin>>arr[i];
sum+=arr[i];
}
long long summ=sum/2;
bool t[n+1][summ+1];
//memset(t,false,sizeof(t));
for(int i=0;i<n+1;i++)
t[i][0]=true;
for(int i=1;i<summ+1;i++)
t[0][i]=false;
for(int i=1;i<n+1;i++)
{
for(int j=1;j<summ+1;j++)
{
if(arr[i-1]<=j)
{
t[i][j]=t[i-1][j-arr[i-1]]||t[i-1][j];
}
else
t[i][j]=t[i-1][j];
}

    }
  
    long long j=summ;
    while(t[n][j]!=true)
    {
        j--;

    }


    long long ans=j;
    cout<<sum-(2*ans)<<endl;

}
}