Description
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.
Input
Each line in the input file describes one collection of marbles to be divided. The lines contain six non-negative integers n1 , . . . , 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 collection, 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.
Sample Input
1 0 1 2 0 0 1 0 0 0 1 1 0 0 0 0 0 0
Sample Output
Collection #1: Can't be divided. Collection #2: Can be divided.
Source
第一种解法,套用前面几篇博文提到的背包问题揭发。程序如下:
// acm2.cpp : 定义控制台应用程序的入口点。
//
#include <iostream>
using namespace std;
#define MAXNUM 6
#define MAXV 100000
int F[MAXV];
int max(int i,int j){
return i>j?i:j;
}
void zeroPack(int v,int weight,int value){
for(int i=v;i>=weight;i--){
F[i]=max(F[i],F[i-weight]+value);
}
}
void completePack(int v,int weight,int value){
for(int i=weight;i<=v;i++){
F[i]=max(F[i],F[i-weight]+value);
}
}
void multiplePack(int v,int weight,int value,int num){
if(weight*num>=v)
completePack(v,weight,value);
else{
int count=num,k=1;
while(k<count){
zeroPack(v,k*weight,k*value);
count-=k;
k=k+k;
}
zeroPack(v,count*weight,count*value);
}
}
int main(){
int n[10],i=0,totalNum=0,k=1;
while(true){
int bin=0;
for(i=1;i<=MAXNUM;i++){
cin>>n[i];
totalNum+=n[i]*i;
bin|=n[i];
}
if(!bin)
break;
F[0]=0;
for(i=1;i<MAXV;i++)
F[i]=0xffffffff;
if(totalNum&0x01){
cout<<"Collection #"<<k<<":\n"<<"Can't be divided."<<endl<<endl;
}else{
int num=totalNum>>1;
for(i=1;i<=MAXNUM;i++){
multiplePack(num,i,i,n[i]);
}
if(F[num]==num){
cout<<"Collection #"<<k<<":\n"<<"Can be divided."<<endl<<endl;
}
else
cout<<"Collection #"<<k<<":\n"<<"Can't be divided."<<endl<<endl;
}
k++;
}
}
第二种解法:DFS
// acm.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include<iostream>
using namespace std;
#define NUMMAX 6
int n[NUMMAX];
bool flag=false;
void DFS(int value,int numMax,int weight){
if(value==weight){
flag=true;
return;
}
for(int i=numMax;i>=1;i--){
if(n[i]){
if(i+value<=weight){
n[i]--;
DFS(value+i,i,weight);
if(flag==true)
break;
}
}
}
}
int main(){
int numMax=0;
for(int i=1;i<=6;i++)
cin>>n[i];
for(int i=1;i<=6;i++)
numMax+=n[i]*i;
if(numMax%2!=0)
cout<<"no "<<endl;
else{
DFS(0,6,numMax/2);
if(flag==false)
cout<<"no"<<endl;
else
cout<<"yes"<<endl;
}
}