POJ 1014 Dividing

Dividing

Time Limit: 1000MS

 

Memory Limit: 10000K

Total Submissions: 64066

 

Accepted: 16604

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,2,3,4,5,6.输入Marsha and Bill拥有每种石头的总和,输出是否满足在不切割石头的情况下,使得他们两人所分得石头价值相同,如果满足输出Can be divided.,否则输出Can't be divided.

解题思路:这是一个多重背包问题,如果单纯转换成01背包问题解决会出现超时的情况。所以我采用两种方法分别对其进行优化。

1.采用二进制的方法对其进行优化,方法是:将第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背包问题,是很大的改进。

<span style="font-size:14px;">#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
int v,bag[120005];
void fun(int value,int num)
{

		int k=1;
		while(k<num)
		{
			for(int i=v;i>=value*k;i--)//这是利用二进制转换的核心代码  
			{
				bag[i]=max(bag[i],bag[i-value*k]+value*k);//采用的是01背包的思想
		         }
				num-=k;
				k<<=1;
			
		}
		for(int i=v;i>=value*num;i--)
		{
	bag[i]=max(bag[i],bag[i-value*num]+value*num);//将剩余的计算进去
		}
	
}
int main()
{
	int k=0,a[10],i,j;
	while(1)
	{
		int sum=0;
		for(i=1;i<=6;i++)
		{
			scanf("%d",&a[i]);
			sum+=a[i]*i;
		}
		if(sum==0)break;
		printf("Collection #%d:\n",++k);
		if(sum%2)
		{
			printf("Can't be divided.\n\n");
		
		}
		else{
		v=sum/2;//由于sum是偶数,这道题有一种情况不用考虑
		memset(bag,0,sizeof(bag));
		for(i=1;i<=6;i++)
		{
			fun(i,a[i]);
		}
		if(bag[v]==v)printf("Can be divided.\n\n");
		else printf("Can't be divided.\n\n");
	    }
	}
	return 0;
}
</span>


 

2.采用bfs,其实这道题用bfs跑得挺快

<span style="font-size:14px;">#include<stdio.h>
int v,a[10],flag;
void bfs(int sum,int n)
{
	if(flag)
	return ;
	if(sum==v)
	{
	flag=1;
	return ; 
    }
	for(int i=n;i>=1;i--)
	{
		if(a[i])
		if(sum+i<=v)
		{
			a[i]--;
			bfs(sum+i,i);
			if(flag)
			return ;
		}
	}
	return ;
}
int main()
{
	int i,k=0;
	while(1)
	{
		v=0;
		for(i=1;i<=6;i++)
		{
			scanf("%d",&a[i]);
			v+=a[i]*i;
		}
		if(v==0)break;
		printf("Collection #%d:\n",++k);
		if(v%2){
		printf("Can't be divided.\n\n");	
		}
		else{
			v=v/2;
			flag=0;
			bfs(0,6);
			if(flag)printf("Can be divided.\n\n");
			else printf("Can't be divided.\n\n");
		}
	} 
	return 0;
}
</span>



 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值