HDU 1059 Dividing 多重背包二进制优化

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

题目:

Dividing

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 20602 Accepted Submission(s): 5798

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 eitherCan 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.

思路:

有面值分别为1,2,3,4,5,6的6种货币,输入相应的数据,输出能否均分两份。
其实是一个多重背包问题,背包容量为货币总价值的一半。
难点在于当数据太大的时候,循环次数太多会超时,这里我们使用二进制优化的思想,可以大大的缩小时间复杂度,从O(n)下降到O(logn)。

这里解释下二进制的原理。
任意数n,都能写成二进制的形式。
比如数20,由1,2,4,8,12可以组成所有20以内的数。
比如1000,由1,2,4,8,16,32,64,128,256,489可以组成所有1000以内的数。
当我们需要将一种物品一个一个添加的时候,添加多次就显得有些浪费时间了,而且仔细想想,如果把这些单个物品看作不一样的物品,这样添加和01背包并无什么差别。
给1000个相同物品不优化就像01背包求解一样要算1000次,加上每次添加一个物品要跑完整个容量,需要的时间就是10^6,如果我们把这1000个数按数二进制优化只需要算10次,因为每个容量都会跑一遍,所以所有组合数都会顾及到,而不会有遗漏,所需要的时间也只是10^4而已。
二进制代码如下:

int rem=num[i];
int nu=1;
int val=value[i]*nu;
while(nu<rem)
{
    for(int j=m; j>=nu*value[i]; j--)
    {
        dp[j]=max(dp[j],dp[j-val]+val);
    }
    rem=rem-nu;
    nu=nu*2;
    val=val*2;
}
nu=rem;
val=value[i]*nu;
for(int j=m; j>=nu*value[i]; j--)
{
    dp[j]=max(dp[j],dp[j-val]+val);
}

其中rem代表剩下的个数,用于求出最后一个二进制数后剩下的数目,当作最最后的一个物品。
nu和val代表当前二进制数情况下由多少个物品组合和组成的价值。
最后再把剩下的物品跑一次01背包,就计算完了所有情况。

代码:

#include <iostream>
#include"string.h"
#include"stdlib.h"
#include"cstdio"

int num[10];
int value[10]= {0,1,2,3,4,5,6};
int dp[120005];
using namespace std;

int main()
{
    int n=0;
    while(scanf("%d %d %d %d %d %d",&num[1],&num[2],&num[3],&num[4],&num[5],&num[6])!=EOF)
    {
        n++;
        if(num[1]==0&&num[2]==0&&num[3]==0&&num[4]==0&&num[5]==0&&num[6]==0)    break;
        memset(dp,0,sizeof(dp));
        int maxn=0;
        for(int i=1; i<=6; i++)
        {
            maxn=maxn+num[i]*value[i];
        }
        int m=maxn/2;
        if(maxn%2==1)
        {
            printf("Collection #%d:\nCan't be divided.\n\n",n);
            continue;
        }
        for(int i=1; i<=6; i++)
        {
            if(num[i]*value[i]>=m)  //相对有限的容量,有限的个数用不完,
            //可在满足容量条件下自由取,等价于无数个,转化成完全背包。
            {
                for(int j=value[i]; j<=m; j++)
                {
                    dp[j]=max(dp[j],dp[j-value[i]]+value[i]);
                }
            }
            else    //将当前物品按二进制进行分块,看作01背包处理。
            {
                int rem=num[i];
                int nu=1;
                int val=value[i]*nu;
                while(nu<rem)
                {
                    for(int j=m; j>=nu*value[i]; j--)
                    {
                        dp[j]=max(dp[j],dp[j-val]+val);
                    }
                    rem=rem-nu;
                    nu=nu*2;
                    val=val*2;
                }
                nu=rem;
                val=value[i]*nu;
                for(int j=m; j>=nu*value[i]; j--)
                {
                    dp[j]=max(dp[j],dp[j-val]+val);
                }
            }
        }
        if(dp[m]==m)    printf("Collection #%d:\nCan be divided.\n\n",n);
        else printf("Collection #%d:\nCan't be divided.\n\n",n);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值