HDU 1059 Dividing (多重背包二进制优化)2021-08-12

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

题意:有六种玻璃球 价值分别为1~6,分别输入六种玻璃球的数量,问总价值是否能均分成价值相等的两份(总值为奇数直接排除)。

对于这道题,我一开始用的是三重循环枚举背包,结果在意料之中,TLE。。

//妥妥的超时代码,数据范围大,而且是三重循环,想想时间复杂度有多大
#include <iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>

using namespace std;
const int M = 60000;
//为什么数组要开60000+呢 假设玻璃球数量的最大值20000全部集中到价值为6的球上,总价值就是120000,均分到个人就是60000(j循环的最大范围)
int dp[7][M+10];
int main()
{
    int k=1,marb[7];
    while(1)
    {
        memset(dp,0,sizeof(dp));
        int sum=0;
        for(int i=1;i<=6;i++)
            {cin>>marb[i];sum+=i*marb[i];}//注意总价值的计算
        if(marb[1]==0&&marb[2]==0&&marb[3]==0&&marb[4]==0&&marb[5]==0&&marb[6]==0) break;
        printf("Collection #%d:\n",k++);
        if(sum%2) {printf("Can't be divided.\n\n");}//如果总价值是奇数直接判断不能均分
        else
        {
            sum/=2;//均分到个人要除2
            for(int i=1;i<=6;i++)
                for(int j=0;j<=sum;j++)
                    for(int k=0;k*i<=j&&k<=marb[i];k++)
                        dp[i][j]=max(dp[i][j],dp[i-1][j-k*i]+k*i);//注意传进max内的两个参数,与模板进行比较
            if(dp[6][sum]==sum) printf("Can be divided.\n\n");
            else printf("Can't be divided.\n\n");
        }
    }
    return 0;
}

之后,在上面代码基础上,利用之前学过的多重背包的二进制优化a了这道题

二进制优化思想:
假设有N种物品,其中第i种物品有si个,将每一种物品中的2^k(k=0,1,2,…)个物品打包成一件新的物品(按k的值从小到大打包)。

例如:第i种物品有200个,则打包成8份,第一份1个,第二份2个,第三份4个,依次8,16,32,64(都是以2为底的多次幂)
此时已经有127个了,还剩200-127=73个第i种物品。
用这些新的物品代替原来第i种物品,后枚举新的物品 选or不选(01背包),就可以拼凑出第i种物品数量的所有方案。
优化之后,对于新的物品运用01背包办法解决。

#include <iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>

using namespace std;
const int maxn = 60000;//sum(max)/2-w[i](min)=60000(最内层j循环的范围)
int dp[maxn+10];
const int W = 100;//玻璃球种类数(6)*log 20000(以2为底,20000的对数)≈85,直接开100
int w[W];//二进制优化后的若干份玻璃球的价值
int main()
{
    int k=1,marb[7];
    while(1)
    {
        memset(dp,0,sizeof(dp));
        memset(w,0,sizeof(w));
        int sum=0;
        int cnt=0;//新物品编号
        for(int i=1;i<=6;i++)//二进制优化
        {
            cin>>marb[i];
            int s=marb[i];
            sum+=i*marb[i];
            int k=1;
            while(k<=s)
            {
                cnt++;
                w[cnt]=i*k;
                s-=k;
                k*=2;
            }
            if(s>0) {cnt++;w[cnt]=i*s;}
        }
        int num=cnt;
        if(marb[1]==0&&marb[2]==0&&marb[3]==0&&marb[4]==0&&marb[5]==0&&marb[6]==0) break;
        printf("Collection #%d:\n",k++);
        if(sum%2) {printf("Can't be divided.\n\n");}//奇数不能均分
        else//优化之后01背包处理
        {
            sum/=2;
            for(int i=1;i<=cnt;i++)
                for(int j=sum;j>=w[i];j--)
                        dp[j]=max(dp[j],dp[j-w[i]]+w[i]);//注意传进max内的两个参数
            if(dp[sum]==sum) printf("Can be divided.\n\n");
            else printf("Can't be divided.\n\n");
        }
    }
    return 0;
}

末尾放一个博主的母函数做法链接,还没学,以后再看吧
HDU 1059母函数做法

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值