【背包专题】G - Dividing hdu 1059【多重背包】

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:\n",++t);
        if(dp[v] == ans-v)
            printf("Can be divided.\n\n");
        else
            printf("Can‘t be divided.\n\n");
    }
    return 0;
}
时间: 2024-10-10 06:29:04

【背包专题】G - Dividing hdu 1059【多重背包】的相关文章

HDU 1059 多重背包+二进制优化

Dividing Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 16909    Accepted Submission(s): 4729 Problem Description Marsha and Bill own a collection of marbles. They want to split the collection

hdu 1059 (多重背包) Dividing

这里;http://acm.hdu.edu.cn/showproblem.php?pid=1059 题意是有价值分别为1,2,3,4,5,6的商品各若干个,给出每种商品的数量,问是否能够分成价值相等的两份. 联想到多重背包,稍微用二进制优化一下.(最近身体不适,压力山大啊) 1 #include<iostream> 2 #include<cstring> 3 #include<cstdio> 4 #define inf 70000 5 using namespace s

hdu 1059 多重背包

题意:价值分别为1,2,3,4,5,6的物品个数分别为a[1],a[2],a[3],a[4],a[5],a[6],问能不能分成两堆价值相等的. 解法:转化成多重背包 1 #include<stdio.h> 2 #include<string.h> 3 #include<algorithm> 4 #include<iostream> 5 using namespace std; 6 7 int dp[120010]; 8 int a[10]; 9 10 int

hdu 1171 Big Event in HDU(母函数|多重背包)

http://acm.hdu.edu.cn/showproblem.php?pid=1171 题意:有n种物品,给出每种物品的价值和数目,要将这些物品尽可能的分成相等的两份A和B且A>=B ,输出A,B. 母函数可以过,但感觉最直接的方法应该是多重背包. 母函数的话,也是按总价值的一半求,从一半到小枚举,直到找到系数不为0的就是B. #include <stdio.h> #include <iostream> #include <map> #include <

hdu 5445 多重背包

Food Problem Time Limit: 3000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 1243    Accepted Submission(s): 368 Problem Description Few days before a game of orienteering, Bell came to a mathematician to sol

HDU 1085 多重背包转化为0-1背包问题

题目大意: 给定一堆1,2,5价值的硬币,给定三个数表示3种价值硬币的数量,任意取,找到一个最小的数无法取到 总价值为M = v[i]*w[i](0<=i<3) 那么在最坏情况下M个数都能取到 , M+1必然取不到 所以给M+1个背包,往里面塞东西,最后由前往后检测,找到第一个无法取满的背包的体积 这道题目里,每一种物品的v*w必然小于M+1,所以不出现完全背包的情况,全部采用多重背包转化为0-1背包解决问题即可 1 #include <cstdio> 2 #include <

poj3211Washing Clothes(字符串处理+01背包) hdu1171Big Event in HDU(01背包)

题目链接: poj3211  hdu1171 这个题目比1711难处理的是字符串如何处理,所以我们要想办法,自然而然就要想到用结构体存储,所以最后将所有的衣服分组,然后将每组时间减半,看最多能装多少,最后求最大值,那么就很愉快的转化成了一个01背包问题了.... hdu1711是说两个得到的价值要尽可能的相等,所以还是把所有的价值分为两半,最后01背包,那么这个问题就得到了解决.. 题目: Washing Clothes Time Limit: 1000MS   Memory Limit: 13

ACM学习历程—HDU 1059 Dividing(dp &amp;&amp; 多重背包)

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 spl

hdu 2191 多重背包 悼念512汶川大地震遇难同胞——珍惜现在,感恩生活

http://acm.hdu.edu.cn/showproblem.php?pid=2191 New~ 欢迎“热爱编程”的高考少年——报考杭州电子科技大学计算机学院关于2015年杭电ACM暑期集训队的选拔 悼念512汶川大地震遇难同胞——珍惜现在,感恩生活 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 17930    Accepted