HDU1059 Dividing
原题链接http://acm.hdu.edu.cn/showproblem.php?pid=1059
Problem Discription
Marsha and Bill own a collection of marbles. They want to split the collection among themselves so that both receive an equal share of the marbles. This would be easy if all the marbles had the same value, because then they could just split the collection in half. But unfortunately, some of the marbles are larger, or more beautiful than others. So, Marsha and Bill start by assigning a value, a natural number between one and six, to each marble. Now they want to divide the marbles so that each of them gets the same total value.
Unfortunately, they realize that it might be impossible to divide the marbles in this way (even if the total value of all marbles is even). For example, if there are one marble of value 1, one of value 3 and two of value 4, then they cannot be split into sets of equal value. So, they ask you to write a program that checks whether there is a fair partition of the marbles.
Unfortunately, they realize that it might be impossible to divide the marbles in this way (even if the total value of all marbles is even). For example, if there are one marble of value 1, one of value 3 and two of value 4, then they cannot be split into sets of equal value. So, they ask you to write a program that checks whether there is a fair partition of the marbles.
Input
Each line in the input describes one collection of marbles to be divided. The lines consist of six non-negative integers n1, n2, ..., n6, where ni is the number of marbles of value i. So, the example from above would be described by the input-line ``1 0 1 2 0 0''. The maximum total number of marbles will be 20000.
The last line of the input file will be ``0 0 0 0 0 0''; do not process this line.
The last line of the input file will be ``0 0 0 0 0 0''; do not process this line.
Output
For each colletcion, output ``Collection #k:'', where k is the number of the test case, and then either ``Can be divided.'' or ``Can't be divided.''.
Output a blank line after each test case.
Output a blank line after each test case.
大致意思就是有一堆价值为1~6的marbles,把它们分成两堆同样价值的marbles,问能不能分。 比如1,3,4,4的组合虽然和为偶数,但不能分出总价值相等的两堆。
问题可以转化为 给定一堆marbles的价值,问能不能选出其中一些marbles,使其总价值为v,v是所有marbles的价值总和的一半(如果价值总和为奇数,当然肯定不能分)。和典型的0-1背包问题不一样的是,这里选marbles必须要使总价值刚好凑到v。
用一个一维数组就能完成,dp[]的类型是bool,dp[j]是到当前那颗marble i为止,能否凑到总价值为j。dp[ j ] = dp[ j ] || dp[ j - value [ i ]]
#include <iostream>
using namespace std;
int main() {
int marbles[20000];
bool dp[120000];
for (int k=1;;k++){
int i =0;
int sum=0;
while (cin >> marbles[i]){
sum += marbles[i];
if (cin.get()=='\n')
break;
i++;
}
if (sum==0) break;
i--;
if (sum %2 != 0){
cout << "Collection #" <<k << ":\n" << "Can't be divided." << endl << endl;
continue;
}
int v = sum/2;
for (int j=1;j<=v;j++ ){
dp[j]=false;
}
dp[0] =true;
for (int t=0;t<=i;t++){
for (int j=v;j>=marbles[t];j--){
dp[j]=dp[j]||dp[j-marbles[t]];
}
}
cout << "Collection #" << k<<":\n";
if (dp[v]==false) cout << "Can't be divided." << endl << endl;
else cout << "Can be divided." << endl << endl;
}
return 0;
}