zoukankan      html  css  js  c++  java
  • hdu 1059 Dividing

    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.
     


    题解:

       第一次写多重背包的问题,其中要考虑是否能被均分,用到物品的价值和所占体积相等,使得判断更加简单。不多说,上代码。

    #include <iostream>
    #include <cstdio>
    #include <cmath>
    #include <cstring>
    using namespace std;
    int dp[120000],V;
    void bag01(int v,int w)                                                //01背包
    {
        for(int i=V; i>=v; i--)
            dp[i]=max(dp[i],dp[i-v]+w);
    }
    void bag11(int v,int w)                                              //完全背包
    {
        for(int i=v; i<=V; i++)
            dp[i]=max(dp[i],dp[i-v]+w);
    }
    void bagmuti(int m,int v,int w)                               //多重背包
    {
        if(m*v>=V)
            bag11(v,w);
        else
        {
            int k=1;
            while(k<m)
            {
                bag01(k*v,k*w);
                m-=k;
                k*=2;
            }
            bag01(m*v,m*w);
        }
    
    }
    
    
    int main()
    {
        int a[7],t=1;
        while(scanf("%d%d%d%d%d%d",&a[1],&a[2],&a[3],&a[4],&a[5],&a[6]),a[1]+a[2]+a[3]+a[4]+a[5]+a[6])
        {
            int sum=a[1]+a[2]*2+a[3]*3+a[4]*4+a[5]*5+a[6]*6;
            if(sum%2)                                                                                           //总价值为奇数,直接判断输出
            {
                printf("Collection #%d:
    Can't be divided.
    
    ",t++);
                continue;
            }
            V=sum/2;
            memset(dp,0,sizeof(dp));
            for(int i=1; i<=6; i++)
                bagmuti(a[i],i,i);                                                                               //体积和价值是一样的
            if(dp[V]==V)                                                                                       //此处为技巧,定义时物件的价值和所需体积是一样的,所以可以这样用
                printf("Collection #%d:
    Can be divided.
    
    ",t++);
            else
                printf("Collection #%d:
    Can't be divided.
    
    ",t++);
    
        }
    
        return 0;
    }
    


  • 相关阅读:
    优先队列的一种实现方式——堆
    iOS 批量打包
    Xcode 8 的 Debug 新特性 —- WWDC 2016 Session 410 & 412 学习笔记
    仿淘宝上拉进入详情页交互的实现
    iOS 10 的适配问题
    集成支付宝钱包支付 iOS SDK 的方法与经验
    Quick Touch – 在 iOS 设备运行的 “Touch Bar”
    iOS 开发中的 Tips(一)
    ReactiveCocoa 中 RACSignal 是怎样发送信号
    自定义下拉刷新控件
  • 原文地址:https://www.cnblogs.com/james1207/p/3279980.html
Copyright © 2011-2022 走看看