您的位置:首页 > 其它

HDU Dividing (多重背包+二进制优化)

2014-04-26 16:27 183 查看

Dividing

点击打开题目

Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 9 Accepted Submission(s) : 3

Font: Times New Roman | Verdana | Georgia

Font Size: ← →

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.

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.

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.

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

Statistic |

Submit |
Back

题目大意:

就是每行输入六个数,分别代表价值为1,2,3,4,5,6的物品的个数,要求看这些物品能不能分成价值相同的两部分,如果可以则输出
Can be divided.
不能,输出[code]Can't be divided.

解题思路:

先把总和算出来,然后先判断一下和是奇数还是偶数,如果是奇数则不能,如果是偶数,则让和的一半当做背包的最大容量,物品对应的价值看做所需的体积,看能否恰好装满背包,另外这题数据量庞大需要优化,(二进制优化请参考/article/2405603.html);

代码:
#include<stdio.h>
#include<memory.h>
#define MAX 2000000000
#define N 100000
int p
,dp
,v
,value
;
void set(int dp[],int V)//赋初值,
{
    int i;
    dp[0]=0;
    for(i=1; i<=V; i++)
    {
        dp[i]=MAX;
    }
}
int main()
{
    int t=1,V,n[7],i,j,sum,count,k;
    while(~scanf("%d%d%d%d%d%d",&n[0],&n[1],&n[2],&n[3],&n[4],&n[5])&&(n[0]||n[1]||n[2]||n[3]||n[4]||n[5]))
    {
        sum=0;
        printf("Collection #%d:\n",t++);
        for(i=0; i<6; i++)
            sum=sum+n[i]*(i+1);//求和
        if(sum%2==1)
           {
               printf("Can't be divided.\n\n");
               continue;
           }
        else
        {
            V=sum/2;
            count=0;
            memset(value,0,sizeof(value));
            memset(v,0,sizeof(v));
            for(i=0; i<6; i++)
            {
                for ( k=1; k<=n[i]; k=k<<1)//二进制优化
                {
                    value[count] = k;
                    v[count++] = k*(i+1);
                    n[i] -= k;
                }
                if (n[i] > 0)
                {
                    value[count] = n[i];
                    v[count++] = n[i]*(i+1);
                }
            }
            set(dp,V);
            for(i=0; i<count; i++)
            {
                for(j=V; j>=v[i]; j--)
                {
                    dp[j]=dp[j]<dp[j-v[i]]+value[i]?dp[j]:dp[j-v[i]]+value[i];
                }
            }
            if(dp[V]!=MAX)//判断是否恰好装满(参考背包九讲)
                printf("Can be divided.\n\n");
            else
                printf("Can't be divided.\n\n");
        }
    }
    return 0;
}
另外一种优化方式(摘于网络):

和取模优化

# include <iostream>
# include <string.h>
using namespace std;
int x[20005],y[20005];
int main()
{
	int a[7],sum,p=1,i,j,k;
	while(scanf("%d%d%d%d%d%d",&a[1],&a[2],&a[3],&a[4],&a[5],&a[6])!=EOF&&(a[1]||a[2]||a[3]||a[4]||a[5]||a[6]))
	{
		sum=0;
		for(i=1;i<=6;i++)
			sum+=i*(a[i]%60);//取模优化
		if(sum%2!=0)
		{
			printf("Collection #%d:\nCan't be divided.\n\n",p);
			p++;
		}
		else
		{
			sum=sum/2;
		memset(x,0,sizeof(x));
		for(i=0;i<=a[1];i++)
		{
			x[i]=1;
		}
		for(i=2;i<=6;i++)
		{
			for(j=0;j<=sum;j++)
			{
				for(k=0;k<=i*a[i]&&k+j<=sum;k+=i)
				{
					y[j+k]+=x[j];
				}
			}
			for(k=0;k<=sum;k++)
			{
				x[k]=y[k];
				y[k]=0;
			}
		}
		if(x[sum]!=0)
		{
			printf("Collection #%d:\nCan be divided.\n\n",p);
			p++;
		}
		else
		{
			printf("Collection #%d:\nCan't be divided.\n\n",p);
			p++;
		}
		}
	}
	return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: