zoukankan      html  css  js  c++  java
  • Dividing_多重背包

    Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
    Total Submission(s) : 27   Accepted Submission(s) : 12

    Font: Times New Roman | Verdana | Georgia

    Font Size: ← →

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

    和coins那题一模一样的思路,多重背包在不同情况下分为01和完全背包求解!以及用到二进制优化

    #include<iostream>
    #include<stdio.h>
    #include<string.h>
    using namespace std;
    int n[105];
    int dp[100005];
    int max(int a,int b)
    {
        return a>b?a:b;
    }
    void onepack(int c,int w,int v)
    {
        for(int i=v;i>=c;i--)
            dp[i]=max(dp[i],dp[i-c]+w);
    }
    void allpack(int c,int w,int v)
    {
        for(int i=c;i<=v;i++)
            dp[i]=max(dp[i],dp[i-c]+w);
    }
    void mulpack(int c,int w,int n,int v)
    {
        if(c*n>v) allpack(c,w,v);
        else
        {
            int k=1;
            while(k<n)//二进制优化
            {
                onepack(k*c,k*w,v);
                n-=k;
                k*=2;
            }
            onepack(n*c,n*w,v);
        }
    }
    int main()
    {
        int n1,n2,n3,n4,n5,n6;
        int cnt=0;
        while(~scanf("%d%d%d%d%d%d",&n[1],&n[2],&n[3],&n[4],&n[5],&n[6]),n[1]||n[2]||n[3]||n[4]||n[5]||n[6])
        {
            cnt++;
            printf("Collection #%d:
    ",cnt);
            int sum=0;
            sum=n[1]*1+n[2]*2+n[3]*3+n[4]*4+n[5]*5+n[6]*6;
            int div=sum/2;
            if(sum%2==1)
            {
                printf("Can't be divided.
    
    ");
                continue;
            }
            else
            {
                memset(dp,0,sizeof(dp));
                for(int i=1; i<=6; i++)
                    mulpack(i,i,n[i],sum/2);
                if(dp[sum/2]==sum/2) printf("Can be divided.
    
    ");
                else printf("Can't be divided.
    
    ");
            }
    
        }
        return 0;
    }
    

      

  • 相关阅读:
    洛谷P1710 地铁涨价
    洛谷P1371 NOI元丹
    洛谷P2409 Y的积木
    洛谷P3392 涂国旗
    洛谷P3370 【模板】字符串哈希
    洛谷P3379 【模板】最近公共祖先(LCA)
    洛谷P3372 【模板】线段树 1
    洛谷P3373 【模板】线段树 2
    Android(java)学习笔记31:泛型高级之通配符
    Android(java)学习笔记30:泛型接口的概述和使用
  • 原文地址:https://www.cnblogs.com/iwantstrong/p/5732343.html
Copyright © 2011-2022 走看看