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

本文介绍了一种解决多重背包问题的方法——通过将问题转化为01背包问题来实现二进制优化,从而降低时间复杂度。具体步骤包括拆分正整数为2^n之和和剩余数,并据此简化多重背包问题为01背包问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Dividing
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 63646 Accepted: 16487

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.

题目大意:有权值分别为1,2,3,4,5,6的大理石,每种都有若干块,也可能没有,问你能否把它们分成权值相等的2份。大理石的总数量不超过20000。

普通的多重背包时间复杂度为O(V*Σn(i)) V为空间容量,n(i)为每种石头的数量限制。题目看上去是多重背包,背包容量为石子权值总和的一半。但是我们可以通过二进制优化将其变成01背包(详见背包九讲V2.0章节2.4)。
所谓的二进制优化就是把一个正整数拆成2^n之和和剩余的数。比如17,我们先拆分出1,然后是2、4、8,这是我们发现如果再往后拆的话1+2+4+8+16=31>17,所以拆分不出16,就只能剩下一个17-1-2-4-8=2了。所以我们把17拆分成了1,2,4,8,2,从1到17的所有正整数都能由着这5个数相加而成。

于是我们把多重背包简化成了01背包,把多重背包每种物品取的数量转化成了若干个数的取与不取。最终我们把问题化成了01背包,时间复杂度下降很多。

#include<stack>
#include<queue>
#include<cmath>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#pragma commment(linker,"/STACK: 102400000 102400000")
#define lson a,b,l,mid,cur<<1
#define rson a,b,mid+1,r,cur<<1|1
using namespace std;
typedef long long LL;
const double eps=1e-6;
const int MAXN=131000;

int dp[MAXN],value[MAXN],n[8],sum;

int main()
{
#ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
#endif // ONLINE_JUDGE
    int cnt=0;
    while(scanf("%d%d%d%d%d%d",&n[1],&n[2],&n[3],&n[4],&n[5],&n[6])!=EOF)
    {
        if(n[1]+n[2]+n[3]+n[4]+n[5]+n[6]==0)
            break;
        if(cnt)
            printf("\n");
        sum=n[1]*1+n[2]*2+n[3]*3+n[4]*4+n[5]*5+n[6]*6;
        int half=sum/2;
        int c=1;
        memset(value,0,sizeof(value));
        memset(dp,0,sizeof(dp));
        for(int i=1;i<=6;i++)
        {
            for(int j=1;j<=n[i];j*=2)//二进制优化,每次拆分出一个2^j
            {
                value[c++]=j*i;
                n[i]-=j;
            }
            if(n[i]>0)//剩余的数单独计算
                value[c++]=n[i]*i;
        }
        for(int i=1;i<=c-1;i++)//背包容量为石子权值总和的一半
            for(int j=half;j>=value[i];j--)
                dp[j]=max(dp[j],dp[j-value[i]]+value[i]);
        if(dp[half]==sum-half)
            printf("Collection #%d:\nCan be divided.\n",++cnt);
        else
            printf("Collection #%d:\nCan't be divided.\n",++cnt);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值