POJ 1014 Dividing(多重背包)

时间:2024-04-22 23:06:36
Dividing

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.

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.

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. 这道题目多重背包入手真心很简单,题目要求,将弹珠根据价值的大小均分给两个人。
多重背包的模板题目里,问题的相关条件有:背包的体积、 物品的种类、 每种物品的数量、 每种物品所占的体积。这是通常情况,而这道题目里,只有
物品的种类、 每种物品的数量、 每种物品所占的体积这3个条件,但是题目也要求简单,就是看这堆弹珠是否能够均分,所以,背包的体积你可以当作是题目极限条件那么大。
然后运用二进制的思想写出多重背包就好.其实我也就是昨天才学会了多重背包。
 #include <cstring>
#include <cstdio>
#include <algorithm>
#include <iostream> using namespace std;
const int max_size = * + ;
int main()
{
int val[];//val数组里存放每种弹珠的数量
int dp[max_size];//dp数组开题目极限那么大 int cas = ; while(cas)
{
int tag = ;
for(int i = ; i < ; i++)
{
scanf("%d", val+i);
if(val[i] == )
{
tag++;
}
}
memset(dp, , sizeof(dp));
if(tag == )
break;
else
{
int tot = ;
for(int i = ; i < ; i++)
{
tot += val[i] * (i+);
}
int half = tot / ;
int half1 = tot - half;
if(half == half1)
{
bool flag = false;
for(int i = ; i < ; i++)
{
int k = ;
while(k < val[i])
{
for(int j = max_size; j - (i+)*k>= ; j--)
{
dp[j] = max(dp[j], dp[j-(i+)*k]+(i+)*k);
if(dp[j] == half)//在dp过程中,找寻是否有一种状态,满足将弹珠平分这一条件
{
flag = true;
break;
}
}
val[i] -= k;
k *= ;
if(flag == true)
break;
}
if(flag != true)
{
for(int j = max_size; j - val[i]*(i+) >= ; j--)
{
dp[j] = max(dp[j], dp[j-(i+)*val[i]]+(i+)*val[i]);
if(dp[j] == half)
{
flag = true;
break;
}
}
}
else
{
printf("Collection #%d:\n", cas);
printf("Can be divided.\n");
break;
}
}
if(flag != true)
{
printf("Collection #%d:\n", cas);
printf("Can't be divided.\n");
}
}
else
{
printf("Collection #%d:\n", cas);
printf("Can't be divided.\n");
}
}
cas++;
printf("\n");//GG,我去,因为没看要多输出一行空行,PE一次
} return ;
}