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
多重背包加二进制优化问题
背包容量为物品总价值的一半,如果总价值为奇数则肯定不可以分
#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;
}

  

HDU 1059(多重背包加二进制优化)的更多相关文章

  1. 动态规划:HDU2844-Coins(多重背包的二进制优化)

    Coins Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Subm ...

  2. hdu 1059 (多重背包) Dividing

    这里;http://acm.hdu.edu.cn/showproblem.php?pid=1059 题意是有价值分别为1,2,3,4,5,6的商品各若干个,给出每种商品的数量,问是否能够分成价值相等的 ...

  3. dp之多重背包(二进制优化)

    void solve(int v,int w,int c){    int count=0;    for(int k=1;k<=c;k<<=1)    {        val[c ...

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

    题目网址:http://poj.org/problem?id=1276 思路: 很明显是多重背包,把总金额看作是背包的容量. 刚开始是想把单个金额当做一个物品,用三层循环来 转换成01背包来做.T了… ...

  5. luogu||P1776||宝物筛选||多重背包||dp||二进制优化

    题目描述 终于,破解了千年的难题.小FF找到了王室的宝物室,里面堆满了无数价值连城的宝物……这下小FF可发财了,嘎嘎.但是这里的宝物实在是太多了,小FF的采集车似乎装不下那么多宝物.看来小FF只能含泪 ...

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

    https://cn.vjudge.net/problem/1365218/origin 题意 一条直线上有n棵树 每棵树上有ci只鸟 在一棵树底下召唤一只鸟的魔法代价是costi 每召唤一只鸟,魔法 ...

  7. HDU-2844 Coins 多重背包 物品数量二进制优化

    题目链接:https://cn.vjudge.net/problem/HDU-2844 题意 给你一些不同价值和一定数量n的硬币. 求用这些硬币可以组合成价值在[1 , m]之间的有多少. 思路 多重 ...

  8. hdu 1059 多重背包

    题意:价值分别为1,2,3,4,5,6的物品个数分别为a[1],a[2],a[3],a[4],a[5],a[6],问能不能分成两堆价值相等的. 解法:转化成多重背包 #include<stdio ...

  9. Dividing (hdu 1059 多重背包)

    Dividing Sample Input 1 0 1 2 0 0 价值为1,2,3,4,5,6的物品数目分别为 1 0 1 2 0 0,求能否将这些物品按价值分为两堆,转化为多重背包.1 0 0 0 ...

随机推荐

  1. DOM基础操作(一)

    DOM的基本操作有四种,我们会逐一给大家进行展示 增加操作 1.创建元素节点 createElement 我们可以通过document.createElement(‘div’);这个方法来创建一个元素 ...

  2. BZOJ4671:异或图

    传送门 直接求连通的不好做,考虑容斥 设 \(g_i\) 表示至少有 \(i\) 个连通块的方案数,\(f_i\) 表示恰好有 \(i\) 个的 那么 \[g_x=\sum_{i=x}^{n}\beg ...

  3. bzoj P3884 上帝与集合的正确用法

    Description   根据一些书上的记载,上帝的一次失败的创世经历是这样的: 第一天, 上帝创造了一个世界的基本元素,称做“元”. 第二天, 上帝创造了一个新的元素,称作“α”.“α”被定义为“ ...

  4. Win10安装docker步骤

    最近使用docker部署spring boot项目,写篇文章记录下步骤. 1. 确保本机win10系统虚拟化已启动启动,否则需要去BIOS设置(方法可百度) 2. 到docker网站下载DockerT ...

  5. 已有Web项目添加Maven支持

    IDE:MyEclipse 当我们在现有的Web开发项目中集成 Maven 的时候,需要修改以下几个地方: 1.将以下代码拷贝到工程根路径下的 .project 文件中的 <buildSpec& ...

  6. Visual Studio 2015 安装笔记

  7. 软工读书笔记 week4 ——《黑客与画家》下

    因为时间有限,只对书中后半部分几个篇章进行了阅读.        一.另一条路       作者以他自己为例,在那个没人知道什么叫“软件运行在服务器时”的时代,他和朋友选择创业时,没有选择写传统的桌面 ...

  8. 泛型< ? extends > <? super> 理解

    public class Test { public static void main(String [] args){ Plate<? extends Fruit> p = new Pl ...

  9. 读<css世界>笔记之img标签

    Web开发时,为了节约带宽以及提高加载性能,首屏以下的图片就会通过滚屏加载的方式异步加载,然后这个即将被异步加载的图片为了布局稳健,体验良好,往往会使用一张透明的图片占位,如: <img src ...

  10. c#多线程调用有参数的方法

      Thread (ParameterizedThreadStart) 初始化 Thread 类的新实例,指定允许对象在线程启动时传递给线程的委托.   Thread (ThreadStart) 初始 ...