Dividing
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 29072 Accepted Submission(s): 8339
Problem 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.
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.
Sample Input
1 0 1 2 0 0
1 0 0 0 1 1
0 0 0 0 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.
Can't be divided.
Collection #2:
Can be divided.
Source
【分析】多重背包+二进制优化
题意:有6种大理石制品,第i种的单位价值为i。已知这6种大理石制品各有ni(i=1, 2, 3, 4, 5, 6)单位,问是否可以将他们划分为两部分,这两部分总价值相同。
此问题可抽象为多重背包问题,只是每种物品”费用“和”价值“均为i。
我们可以求出这6种大理石制品的总价值sumval,当sumval为奇数时显然是不能按题意划分的;当sumval为偶数时是有可能完成划分的。
由于数据规模限制:The maximum total number of marbles will be 20000. -> 大理石制品最高可达20000单位,故总价值最高可达120000。因此应考虑二进制优化后将多重背包问题转换为01背包问题求解。
二进制优化核心:将第i种物品分成若干件物品,其中每件物品有一个系数,这件物品的费用和价值均是原来的费用和价值乘以这个系数。使这些系数分别为1,2,4,...,2^(k-1),n[i]-2^k+1,且k是满足n[i]-2^k+1>0的最大整数(注意:这些系数已经可以组合出1~n[i]内的所有数字)。例如,如果n[i]为13,就将这种物品分成系数分别为1,2,4,6的四件物品。
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int MAX=120005;
int ncase=1;
int ans,sumval; //ans-物品"分组数" sumval-大理石制品总价值
int n[7]; //n[i]-第i种大理石制品的数量
int v[MAX],dp[MAX]; //v[i]-第i组大理石制品的价值 dp[i]-从i组大理石制品种选取一些可获得的最大总价值
int main()
{
int i,j,k;
while(scanf("%d",&n[1])!=EOF)
{
sumval=n[1];
for(i=2;i<=6;i++)
{
scanf("%d",&n[i]);
sumval+=(i*n[i]);
}
//结束输入
if(sumval==0)
break;
printf("Collection #%d:\n",ncase++);
//大理石制品总价值为奇数时,无法划分
if(sumval%2!=0)
{
printf("Can't be divided.\n\n");
continue;
}
//否则可能有可行方案
ans=0;
sumval/=2;
memset(dp,0,sizeof(dp));
//二进制优化
for(i=1;i<=6;i++)
{
//k表示当前指数
k=1;
while(n[i]>=k)
{
++ans;
v[ans]=i*k;
n[i]-=k;
k*=2;
}
//处理剩余部分
if(n[i]>0)
{
++ans;
v[ans]=i*n[i];
}
}
//转换为01背包求解
for(i=1;i<=ans;i++)
for(j=sumval;j>=v[i];j--)
dp[j]=max(dp[j],dp[j-v[i]]+v[i]);
//判断是否可划分
if(dp[sumval]!=sumval)
printf("Can't be divided.\n");
else
printf("Can be divided.\n");
printf("\n");
}
return 0;
}