HDU 1059(多重背包加二进制优化)

http://acm.hdu.edu.cn/showproblem.php?pid=1059

Dividing

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 29901    Accepted Submission(s): 8501

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.

Source

Mid-Central European Regional Contest 1999

多重背包加二进制优化问题

背包容量为物品总价值的一半,如果总价值为奇数则肯定不可以分

#include<bits/stdc++.h>
using namespace std;
#define max_v 120000
#define max_n 7
int num[max_n],f[max_v],v[max_n]={0,1,2,3,4,5,6};
void ZeroOnePack(int cost,int weight,int c)
{
    for(int v=c;v>=cost;v--)
    {
        f[v]=max(f[v],f[v-cost]+weight);
    }
}
void CompletePack(int cost,int weight,int c)
{
    for(int v=cost;v<=c;v++)
    {
        f[v]=max(f[v],f[v-cost]+weight);
    }
}
void MultiplePack(int cost,int weight,int amount,int c)
{
    if(cost*amount>=c)
    {
        CompletePack(cost,weight,c);
        return ;
    }
    int k=1;
    while(k<amount)
    {
        ZeroOnePack(k*cost,k*weight,c);
        amount-=k;
        k*=2;
    }
    ZeroOnePack(amount*cost,amount*weight,c);
}
int main()
{
    int t=0;
    while(1)
    {
        int sum=0,flag=0;
        for(int i=1;i<max_n;i++)
        {
            scanf("%d",&num[i]);
            sum=sum+i*num[i];
        }
        if(sum==0)
            break;
        if(sum%2==1)
        {
             printf("Collection #%d:\nCan‘t be divided.\n\n",++t);
             continue;
        }
        sum=sum/2;
        memset(f,0,sizeof(f));
        for(int i=1;i<max_n;i++)
        {
            MultiplePack(v[i],v[i],num[i],sum);
        }
        //printf("%d\n",f[sum]);
        if(f[sum]==sum)
        {
            flag=1;
        }
        if(flag==1)
        {
            printf("Collection #%d:\nCan be divided.\n\n",++t);
        }else
        {
            printf("Collection #%d:\nCan‘t be divided.\n\n",++t);
        }
    }
    return 0;
}

  

原文地址:https://www.cnblogs.com/yinbiao/p/9141505.html

时间: 2024-12-16 04:43:25

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

多重背包(二进制优化)

链接:https://www.acwing.com/problem/content/5/ 有 N种物品和一个容量是 V 的背包. 第 i 种物品最多有 si 件,每件体积是 vi,价值是 wi. 求解将哪些物品装入背包,可使物品体积总和不超过背包容量,且价值总和最大. 输出最大价值. 输入格式 第一行两个整数,N,V,用空格隔开,分别表示物品种数和背包容积. 接下来有 N 行,每行三个整数 vi,wi,si,用空格隔开,分别表示第 i 种物品的体积.价值和数量. 输出格式 输出一个整数,表示最大

HDU 2844 Coins 多重背包(二进制优化)

点击打开链接 Coins Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 8167    Accepted Submission(s): 3327 Problem Description Whuacmers use coins.They have coins of value A1,A2,A3...An Silverland dolla

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

POJ 1276 Cash Machine(多重背包的二进制优化)

题目网址:http://poj.org/problem?id=1276 思路: 很明显是多重背包,把总金额看作是背包的容量. 刚开始是想把单个金额当做一个物品,用三层循环来 转换成01背包来做.T了-- 后面学习了 用二进制来处理数据. 简单地介绍一下二进制优化:?(? ? ??)  假设数量是8,则可以把它看成是1,2,4,1的组合,即这4个数的组合包括了1-8的所有取值情况.这是为什么呢?将它们转换成二进制再观察一下: 1:1 2:10 4:100 1:1 二进制都只有0,1.所以1,2,4

CodeForces922E DP//多重背包的二进制优化

https://cn.vjudge.net/problem/1365218/origin 题意 一条直线上有n棵树 每棵树上有ci只鸟 在一棵树底下召唤一只鸟的魔法代价是costi 每召唤一只鸟,魔法上限会增加B 从一棵树走到另一棵树,会增加魔法X 一开始的魔法和魔法上限都是W 问最多能够召唤的鸟的个数 显然这是一道DP题 用dp[i][j]来表示到j这个树下选到j只鸟可以获得的最大能量值 很容易得出dp状态转移方程dp[i][j] = max(dp[i][j],dp[i][j - 1] - c

poj1014+hdu1059--A - Dividing(多重背包,二进制优化)

A - Dividing Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status 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

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

hdoj 1059 Dividing 【多重背包】||【优化母函数】

题意:每一种弹珠(marble)的都有各自的价值,第一种为1, 第二种为2,..,给出你每种弹珠的数量,求能不能将价值总和均分. 策略:rt: 这道题比赛的时候没有想到用母函数,就用了多重背包来解,之后递交的时候时间居然超600ms,我再一看递交排行都是0ms(⊙﹏⊙b汗).看到讨论区有人说母函数也可以,于是就写了个普通的,可惜TL了.果然还是需要优化啊...于是游来游去果然0ms(O(∩_∩)O哈哈哈~). 说一下0秒的思路,因为只是问能不能均分,所以我们没有必要找种类数,找到了就定义为1好了