zoukankan      html  css  js  c++  java
  • 【背包专题】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;
    }
  • 相关阅读:
    CQD(陈丹琦)分治 & 整体二分——专题小结
    [联赛可能考到]图论相关算法——COGS——联赛试题预测
    C++ 线段树—模板&总结
    树形动态规划(树状DP)小结
    树形动态规划(树形DP)入门问题—初探 & 训练
    哈希表(散列表),Hash表漫谈
    随机系列生成算法(随机数生成)
    什么是动态规划算法,常见的动态规划问题分析与求解
    数学之美系列二十四 -- 谈谈动态规划与如何设计动态规划算法
    owasp zap 安全审计工具 功能详解
  • 原文地址:https://www.cnblogs.com/hellocheng/p/7435428.html
Copyright © 2011-2022 走看看