首页 > ACM题库 > HDU-杭电 > HDU 1059 Dividing-背包问题-[解题报告] Cpp
2013
11-26

HDU 1059 Dividing-背包问题-[解题报告] Cpp

Dividing

问题描述 :

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.

输入:

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.

输出:

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.

样例输入:

1 0 1 2 0 0
1 0 0 0 1 1
0 0 0 0 0 0

样例输出:

Collection #1:
Can't be divided.

Collection #2:
Can be divided.

 

http://acm.hdu.edu.cn/showproblem.php?pid=1059

 

解题思路:给你6种价值不一样的东西,并且每种物品都有一定的数量,现在问你,你可以把他们平均分成两份价值一样的吗??

 

解题思路:多重背包问题

 

比较笨拙的代码

 

#include <stdio.h>
#include <string.h>
#include <math.h>
#define Max(a,b) a>b?a:b
#define Size 100005
int c[2*Size],w[2*Size],dp[200005];
int main()
{
	int sum,i,num[7],no = 1,len,j,k,tem;
	while (1)
	{
		sum = 0;
		for (i=1;i<=6;i++)
		{
			scanf("%d",&num[i]);
			sum+=num[i]*i;
		}
		if(!sum)
			break;
		if(sum&1)
		else
		{
			len = 0;
			for (i=1;i<=6;i++)
			{
				/*二进制划分,转成01背包做*/
				if(num[i])
				{
					k =(int)(log(num[i]+1)/log(2))-1;
					for (j=0;j<=k;j++)
					{
						c[len] = w[len] = (int)(pow(2.0,j)*i);
						len++;
					}
					tem = (int)(pow(2.0,k+1))-1;
					if(num[i]!=tem)
					{
						c[len] = w[len] = (num[i]-tem)*i;
						len++;
					}
				}
			}
			for(i=0;i<=sum;i++)
				dp[i] = 0;
			for (i=0;i<len;i++)
			{
				for (j = sum/2;j>=c[i];j--)
				{
					dp[j] = Max(dp[j],dp[j-c[i]]+w[i]);
				}
			}
			if(dp[sum/2] == sum/2)
				printf("Collection #%d:/nCan be divided./n/n",no++);
			else
				printf("Collection #%d:/nCan't be divided./n/n",no++);
		}
	}
	return 0;
}

 

参考背包问题后,学习的代码

 

#include <stdio.h>
#include <string.h>
#define Size 60005
#define Max(a,b) a>b?a:b
int sum;
int dp[Size];
/*01背包*/
void ZeroOnePack(int cost,int weight)
{
	int i;
	for(i = sum;i>=cost;i--)
		dp[i] = Max(dp[i],dp[i-cost]+weight);
}
/*完全背包*/
void CompletePack(int cost,int weight)
{
	int i;
	for(i=cost;i<=sum;i++)
		dp[i] = Max(dp[i],dp[i-cost]+weight);
}
/*多重背包*/
void MulPack(int cost,int weight,int count)
{
	int i;
	if(cost*count>=sum)
	{
		CompletePack(cost,weight);
		return ;
	}
	i = 1;
	/*二进制划分*/
	while (i<count)
	{
		ZeroOnePack(i*cost,i*weight);
		count-=i;
		i<<=1;
	}
	ZeroOnePack(count*cost,count*weight);
}
int main()
{
	int num[7],no = 1,i;
	while (1)
	{
		sum = 0;
		for(i=1;i<=6;i++)
		{
			scanf("%d",&num[i]);
			sum+=num[i]*i;
		}
		if(!sum)
			break;
		if(sum&1)
			printf("Collection #%d:/nCan't be divided./n/n",no++);
		else
		{
			sum>>=1;
			for(i=0;i<=sum;i++)
				dp[i] = 0;
			for(i=1;i<=6;i++)
				if(num[i])
					MulPack(i,i,num[i]);
			if(dp[sum] == sum)
				printf("Collection #%d:/nCan be divided./n/n",no++);
			else
				printf("Collection #%d:/nCan't be divided./n/n",no++);
		}
	}
	return 0;
}

 


  1. 学算法中的数据结构学到一定程度会乐此不疲的,比如其中的2-3树,类似的红黑树,我甚至可以自己写个逻辑文件系统结构来。

  2. int half(int *array,int len,int key)
    {
    int l=0,r=len;
    while(l<r)
    {
    int m=(l+r)>>1;
    if(key>array )l=m+1;
    else if(key<array )r=m;
    else return m;
    }
    return -1;
    }
    这种就能避免一些Bug
    l,m,r
    左边是l,m;右边就是m+1,r;

  3. #include <stdio.h>
    int main(void)
    {
    int arr[] = {10,20,30,40,50,60};
    int *p=arr;
    printf("%d,%d,",*p++,*++p);
    printf("%d,%d,%d",*p,*p++,*++p);
    return 0;
    }

    为什么是 20,20,50,40,50. 我觉得的应该是 20,20,40,40,50 . 谁能解释下?