#POJ1014#Dividing(多重背包二进制优化)

30 篇文章 0 订阅
3 篇文章 0 订阅
Dividing
Time Limit: 1000MS Memory Limit: 10000K

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

【解】
题目大意:
将一些给定价值和个数的大理石平均分成价值相等的两份,问是否可行。
1,因为是平均分成两份,所以每份的价值为总价值1/2,那么可以将总价值算出来除以二,再做一次背包,看是否能达到一半的总价值。
2,本题的背包给出每种价格的石子数,这是一个多重背包,将石子数看做物品数,石子价值即为物品价值,石子的价值同时也作为物品重量,目的是要尽可能地装满一个容量为一半总价值的背包。传统的多重背包的时间复杂度小于O(NV^),由于本题的数据规模较大,传统的多重背包是会超时的。那么把第i种物品换成n[i]件01背包中的物品,则得到了物品数为Σn[i]的01背包问题,直接求解,复杂度仍然是O(V*Σn[i])。
3,最优的解法应该是采用二进制优化。
"考虑把第i种物品换成若干种物品,使得原问题中第i种物品可取的每种策略——取0..n[i]件——均能等价于取若干件代换以后的物品。另外,取超过n[i]件的策略必不能出现。
方法是:将第i种物品分成若干件物品,其中每件物品有一个系数,这件物品的费用和价值均是原来的费用和价值乘以这个系数。使这些系数分别为 1,2,4,...,2^(k-1),n[i]-2^k+1,且k是满足n[i]-2^k+1>0的最大整数。
例如,如果n[i]为13,就将这种 物品分成系数分别为1,2,4,6的四件物品。分成的这几件物品的系数和为n[i],表明不可能取多于n[i]件的第i种物品。
另外这种方法也能保证对于0..n[i]间的每一个整数,均可以用若干个系数的和表示,这个证明可以分0..2^k-1和2^k..n[i]两段来分别讨论得出,并不难,希望你自己思考尝试一下。
这样就将第i种物品分成了O(log n[i])种物品,将原问题转化为了复杂度为O(V*Σlog n[i])的01背包问题,是很大的改进。
下面给出O(log amount)时间处理一件多重背包中物品的过程,其中amount表示物品的数量:
procedure MultiplePack(cost,weight,amount)
    if cost*amount>=V
        CompletePack(cost,weight)
        return
    integer k=1
    while k<amount
        ZeroOnePack(k*cost,k*weight)
        amount=amount-k
        k=k*2
    ZeroOnePack(amount*cost,amount*weight)
."------摘自《背包九讲》
#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
int tot;
int a[8],f[300010],num[300010];
void pack(int n,int v){
	int i=0,x,tmp=0;
	while(1){
		x=1<<i;
		if(x+tmp>n)
			break;
		tmp+=x;
		num[tot++]=x*v;
		i++;
	}
	x=n-tmp;
	if(x!=0)
		num[tot++]=v*x;
}
int main(){
	int n=1,i,j,vall;
	while(1){
		for(i=1,vall=0;i<=6;i++){
			scanf("%d",&a[i]);
			vall+=a[i]*i;
		}
		if(!vall)break;
		printf("Collection #%d:\n", n++);  
		if(vall%2){
			printf("Can't be divided.\n\n");
			continue;
		}
		vall/=2;
		tot=0;
		for(i=1;i<=6;i++)
			pack(a[i],i);
		for(i=1;i<tot;i++)
			for(j=vall;j>=num[i];j--)
				if(f[j-num[i]]+num[i]>f[j])
					f[j]=f[j-num[i]]+num[i];
		if(f[vall]==vall)printf("Can be divided.\n\n");
		else printf("Can't be divided.\n\n");
		memset(f,0,sizeof(f));
	}
}


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值