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;
    }
    

      

  • 相关阅读:
    (简单) POJ 3414 Pots,BFS+记录路径。
    (简单) POJ 3087 Shuffle'm Up,枚举。
    (简单) POJ 3126 Prime Path,BFS。
    (简单) POJ 1426 Find The Multiple,BFS+同余。
    (简单) POJ 3279 Fliptile,集合枚举。
    (简单) POJ 1278 Catch That Cow,回溯。
    (简单) POJ 2251 Dungeon Master,BFS。
    (简单) POJ 1321 棋盘问题,回溯。
    回溯---输出二叉树中所有从根到叶子的路径
    回溯---在矩阵中寻找字符串
  • 原文地址:https://www.cnblogs.com/iwantstrong/p/5732343.html
Copyright © 2011-2022 走看看