zoukankan      html  css  js  c++  java
  • ACM学习历程—HDU 1059 Dividing(dp && 多重背包)

    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.
           

    题目大意就是判断这么多数字能不能均分成两类,每个数字不可拆分。

    看完题目就感觉是个多重背包。不过这里只用判断sum/2能否被装到。于是就不用判断sum/2+1到sum的背包了。

    由于只用判断是否能装到,于是只用开bool型数组即可。

    由于每个数字有一定的使用次数,所以需要开vis数组,而且对于每一种数字的背包需要初始化全为1。

    由于考虑到需要最优解,所以对于dp[j]为真的情况,就不需要再判断dp[j-i]了,因为如果再靠dp[j-i]来放下num[i]的话,就浪费了一次数字i的使用。

    代码:

    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <cmath>
    #define N 1000000007
    
    using namespace std;
    
    int num[7], sum;
    int vis[60005];
    bool dp[60005];
    
    bool Input()
    {
        sum = 0;
        memset(dp, 0, sizeof(dp));
        for (int i = 1; i <= 6; ++i)
        {
            scanf("%d", &num[i]);
            sum += i*num[i];
        }
        if (sum)
            return true;
        else
            return false;
    }
    
    void Work()
    {
        if (sum % 2)
        {
            printf("Can't be divided.
    
    ");
            return;
        }
        sum /= 2;
        dp[0] = true;
        for (int i = 1; i <= 6; ++i)
        {
            if (num[i] == 0)
                continue;
            memset(vis, 0, sizeof(vis));
            for (int j = i; j <= sum; j++)
            {
                if (dp[j-i] && !dp[j] && vis[j-i] < num[i])
                {
                    dp[j] = true;
                    vis[j] = vis[j-i]+1;
                }
            }
        }
        if (dp[sum])
            printf("Can be divided.
    
    ");
        else
            printf("Can't be divided.
    
    ");
    }
    
    int main()
    {
        //freopen("test.in", "r", stdin);
        int times = 1;
        while (Input())
        {
            printf("Collection #%d:
    ", times);
            Work();
            times++;
        }
        return 0;
    }
  • 相关阅读:
    华为靠近获准为伦敦地铁供给移动收集
    Solaris 10装置jdk1.6及改削成默许JDK
    “沃Phone”生于忧患 联通推另类新规
    Android 3.0终极版SDK正式颁发颁发
    MyGeneration连接MySql数据库的处理步履
    解析:百思买败于外部派系争斗
    联发科月度付出跌至四年来冰点
    AT&T:iPhone带来无线营业量收齐打破
    阐发师以为移动网络运营商需斥地数据获利新途径
    MSSQL按分页前去查询成绩的存储历程
  • 原文地址:https://www.cnblogs.com/andyqsmart/p/4517673.html
Copyright © 2011-2022 走看看