【POJ】 1014 Dividing(多重背包,优化)
【题目链接】http://poj.org/problem?id=1014
题目
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.
Source
Mid-Central European Regional Contest 1999
大意就是一堆体积为1-6的弹珠,给出每种个数,问能不能平分成两堆;
思路:多重背包,每一层分别转化为完全背包或01背包。
AC代码
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<string>
#include<algorithm>
#include<math.h>
#include<limits.h>
#include<stack>
#include<queue>
#define LL long long
using namespace std;
int p[7]={0};
int dp[220005];
int v;
void Zeroonepack(int cost,int value)//01背包
{
for(int i=v;i>=cost;i--)
dp[i]=max(dp[i],dp[i-cost]+value);
}
void Completepack(int cost,int value)//完全背包
{
for(int i=cost;i<=v;i++)
dp[i]=max(dp[i],dp[i-cost]+value);
}
void Multipack(int cost,int value,int num)//多重背包
{
int k;
if(num*cost>=v)
Completepack(cost,value);
else
{
k=1;
while(num>k)
{
Zeroonepack(k*cost,k*value);
num-=k;
k*=2;
}
Zeroonepack(num*cost,num*value);
}
}
int main()
{
int k=1,sum;
while(scanf("%d%d%d%d%d%d",&p[1],&p[2],&p[3],&p[4],&p[5],&p[6])!=EOF&&!(p[1]==0&&p[2]==0&&p[3]==0&&p[4]==0&&p[5]==0&&p[6]==0))
{
printf("Collection #%d:\n",k++);
sum=0;
for(int i=1;i<7;i++)
{
sum+=i*p[i];
}
if(sum%2!=0)
{
printf("Can't be divided.\n\n");
continue;
}
memset(dp,0,sizeof(dp));
v=sum/2;
for(int i=1;i<=6;i++)
{
Multipack(i,i,p[i]);
}
if(dp[v]==v)//确定是否正好装满;
printf("Can be divided.\n\n");
else printf("Can't be divided.\n\n");
}
return 0;
}
然后还有一个错误代码,我还没看出来为什么错,对于400 400 400 400 400 400输出了can’t
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<string>
#include<algorithm>
#include<math.h>
#include<limits.h>
#include<stack>
#include<queue>
#define LL long long
using namespace std;
int p[7]={0};
int f[120005];
int main()
{
int k=1,sum; while(scanf("%d%d%d%d%d%d",&p[1],&p[2],&p[3],&p[4],&p[5],&p[6])!=EOF&&!(p[1]==0&&p[2]==0&&p[3]==0&&p[4]==0&&p[5]==0&&p[6]==0))
{
printf("Collection #%d:\n",k++);
sum=0;
int v;
for(int i=1;i<7;i++)
{
sum+=i*p[i];
}
if(sum%2!=0)
{
printf("Can't be divided.\n");
continue;
}
memset(f,0,sizeof(f));
v=sum/2;
for(int i=1;i<=6;i++)
{
for(int vv=v;vv>0;vv--)
{
for(int j=1;j<=p[i];j*=2)
{
if(j*i<=vv) f[vv]=max(f[vv],f[vv-j*i]+j*i);
}
}
}
if(f[v]==v)
printf("Can be divided.\n");
else printf("Can't be divided.\n");
}
return 0;
}