• 【背包专题】G


    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. 

    InputEach 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. 
    OutputFor 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,2,3,4,5,6硬币的对应数量,判断能否将硬币组合为总价值的一半,比如:输入1 1 1 1 1 1,说明价值为1,2,3,4,5,6的硬币的数量都同为1,总价值为21,我们组合出得一半是10价值的硬币,另一半是11,所以不能够均分。
    思路:注意最多的数量范围是20000,dp数组最大为120000,所以这道题明显是多重背包。我们将总价值ans计算好以后,除以2作为背包的容量v,dp数组存储v容量下的最大价值,最后判断dp[v]是否等于ans-v即可,不能够直接用dp[v]是否等于v判断,原因参见我给出的题意上的样例
    #include<stdio.h>
    #include<string.h>
    #include<algorithm>
    using namespace std;
    #define N 120010
    int dp[N],v,ans,num[10];
    int value[7] = {0,1,2,3,4,5,6};
    void CompletePack(int v,int value)
    {
        int i;
        for(i = value; i <= v; i ++)
            dp[i] = max(dp[i],dp[i-value]+value);
        return;
    }
    
    void ZeroOnePack(int cost,int value)
    {
        int i;
        for(i = v; i >= cost; i --)
            dp[i] = max(dp[i],dp[i-cost]+value);
        return;
    }
    
    int main()
    {
        int i,j,k,x;
        int t = 0;
        while(scanf("%d%d%d%d%d%d",&num[1],&num[2],&num[3],&num[4],&num[5],&num[6]),(num[1]+num[2]+num[3]+num[4]+num[5]+num[6]))
        {
            ans = 0;
            memset(dp,0,sizeof(dp));
            for(i = 1; i <= 6; i ++)
                ans += num[i]*value[i];
            v= ans/2;
            for(i = 1; i <= 6; i ++)
            {
                if(num[i]*value[i] > v)
                    CompletePack(v,value[i]);
                else
                {
                    k = 1;
                    while(num[i]>0)
                    {
                        x = min(k,num[i]);
                        ZeroOnePack(x*value[i],x*value[i]);
                        num[i]-=x;
                        k*=2;
                    }
                }
            }
            printf("Collection #%d:
    ",++t);
            if(dp[v] == ans-v)
                printf("Can be divided.
    
    ");
            else
                printf("Can't be divided.
    
    ");
        }
        return 0;
    }
  • 相关阅读:
    2.5亿!华为成立新公司!
    两年半换第 4 份工作,做个总结
    不懂什么叫编程?
    Google 为什么把几十亿行代码放在一个库?
    IntelliJ 平台 2020 年路线图
    别找了,这是 Pandas 最详细教程了
    MongoDB是什么?看完你就知道了!
    有了这个神器,轻松用 Python 写 APP !
    整理出来几个比较实用的代码对比工具
    学习进度条 第六十一-七十五天 SpringMVC学习笔记
  • 原文地址:https://www.cnblogs.com/hellocheng/p/7435428.html
Copyright © 2020-2023  润新知