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.
 
解题思路:用多重背包的思路来挑选若干件相同或不同的物品,最后看能否组成总价值的一半即可。
AC代码(432ms):
 #include<bits/stdc++.h>
using namespace std;
int W,tol,cas=,x[],dp[];
void ZeroOnePack(int w,int v){
for(int j=W;j>=w;--j)
dp[j]=max(dp[j],dp[j-w]+v);
}
void CompletePack(int w,int v){
for(int j=w;j<=W;++j)
dp[j]=max(dp[j],dp[j-w]+v);
}
void MultiplePack(int w,int v,int num){
if(w*num>=W)CompletePack(w,v);
else{
for(int k=;k<=num;k<<=){
ZeroOnePack(w*k,v*k);
num-=k;
}
if(num>)ZeroOnePack(w*num,v*num);
}
}
int main(){
while(~scanf("%d%d%d%d%d%d",&x[],&x[],&x[],&x[],&x[],&x[])){
tol=x[]+x[]*+x[]*+x[]*+x[]*+x[]*;
if(!tol)break;
else if(tol&)printf("Collection #%d:\nCan't be divided.\n\n",cas++);
else{
W=tol/;memset(dp,,sizeof(dp));
for(int i=;i<=;++i)
MultiplePack(i,i,x[i]);
if(dp[W]==W)printf("Collection #%d:\nCan be divided.\n\n",cas++);
else printf("Collection #%d:\nCan't be divided.\n\n",cas++);
}
}
return ;
}

AC代码二(312ms):单调队列稍微优化版。

 #include<bits/stdc++.h>
using namespace std;
int W,tol,cas=,x[],dp[];
struct node{
int k,v;
node(int x,int y):k(x),v(y){}
};
deque<node> dq;
void SingleDeque(int w,int v,int cnt){
for(int r=;r<w;++r){//r=j%w
dq.clear();
for(int t=;t*w+r<=W;++t){//t=j/w
int tmp=dp[t*w+r]-t*v;
while(!dq.empty()&&tmp>=dq.back().v)dq.pop_back();
dq.push_back(node(t,tmp));
while(!dq.empty()&&(t-cnt>dq.front().k))dq.pop_front();
dp[t*w+r]=dq.front().v+t*v;
}
}
}
int main(){
while(~scanf("%d%d%d%d%d%d",&x[],&x[],&x[],&x[],&x[],&x[])){
tol=x[]+x[]*+x[]*+x[]*+x[]*+x[]*;
if(!tol)break;
else if(tol&)printf("Collection #%d:\nCan't be divided.\n\n",cas++);
else{
W=tol/;memset(dp,,sizeof(dp));
for(int i=;i<=;++i)
SingleDeque(i,i,x[i]);
if(dp[W]==W)printf("Collection #%d:\nCan be divided.\n\n",cas++);
else printf("Collection #%d:\nCan't be divided.\n\n",cas++);
}
}
return ;
}

AC代码三(78ms):考虑多重部分和解法。dp[i][j]表示前i-1种数加和得到j时第i-1种数最多能剩余多少个(不能加和得到j的情况下为-1)。

 #include<bits/stdc++.h>
using namespace std;
int W,tol,cas=,x[],dp[];
int main(){
while(~scanf("%d%d%d%d%d%d",&x[],&x[],&x[],&x[],&x[],&x[])){
tol=x[]+x[]*+x[]*+x[]*+x[]*+x[]*;
if(!tol)break;
else if(tol&)printf("Collection #%d:\nCan't be divided.\n\n",cas++);
else{
W=tol/;memset(dp,-,sizeof(dp));dp[]=;//注意:初始化加和为0剩下的个数为0
for(int i=;i<=;++i){
for(int j=;j<=W;++j){
if(dp[j]>=)dp[j]=x[i];
else if(j<i||dp[j-i]<=)dp[j]=-;
else dp[j]=dp[j-i]-;
}
}
if(dp[W]>=)printf("Collection #%d:\nCan be divided.\n\n",cas++);
else printf("Collection #%d:\nCan't be divided.\n\n",cas++);
}
}
return ;
}

题解报告:hdu 1059 Dividing(多重背包、多重部分和问题)的更多相关文章

  1. HDU 1059 Dividing 分配(多重背包,母函数)

    题意: 两个人共同收藏了一些石头,现在要分道扬镳,得分资产了,石头具有不同的收藏价值,分别为1.2.3.4.5.6共6个价钱.问:是否能公平分配? 输入: 每行为一个测试例子,每行包括6个数字,分别对 ...

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

    Description Marsha and Bill own a collection of marbles. They want to split the collection among the ...

  3. HDOJ(HDU).1059 Dividing(DP 多重背包+二进制优化)

    HDOJ(HDU).1059 Dividing(DP 多重背包+二进制优化) 题意分析 给出一系列的石头的数量,然后问石头能否被平分成为价值相等的2份.首先可以确定的是如果石头的价值总和为奇数的话,那 ...

  4. hdu 1059 Dividing bitset 多重背包

    bitset做法 #include <bits/stdc++.h> #define PI acos(-1.0) #define mem(a,b) memset((a),b,sizeof(a ...

  5. hdu 1059 Dividing(多重背包优化)

    Dividing Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Su ...

  6. Hdu 1059 Dividing & Zoj 1149 & poj 1014 Dividing(多重背包)

    多重背包模板- #include <stdio.h> #include <string.h> int a[7]; int f[100005]; int v, k; void Z ...

  7. hdu 1059 Dividing

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

  8. HDU 1059 Dividing (dp)

    题目链接 Problem Description Marsha and Bill own a collection of marbles. They want to split the collect ...

  9. hdu 1059 Dividing 多重背包

    点击打开链接链接 Dividing Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

随机推荐

  1. 安装软件:/lib/ld-linux.so.2: bad ELF interpreter解决

    http://linux.chinaitlab.com/set/928509.html 我们在CentOS系统中安装软件:/lib/ld-linux.so.2: bad ELF interpreter ...

  2. javascript闭包诡异的问题

    var funcs = []; for (var i = 0; i < 3; i++) { // let's create 3 functions funcs[i] = function() { ...

  3. C++第9周(春)项目5 - 一元一次方程类

    课程首页在:http://blog.csdn.net/sxhelijian/article/details/11890759,内有完整教学方案及资源链接 [项目5]设计一元一次方程类.求形如ax+b= ...

  4. 无限级分类Asp.net Mvc实现

    无限级分类Asp.net Mvc实现   无限级分类涉及到异步加载子类.加载当前类和匹配问题,现在做一个通用的实现.   (一) 效果如下:   (二)设计.实现及使用 (1)数据库 (a)表设计db ...

  5. LoadRunner系列之—-02 基于webservice协议的接口测试(脚本实例)

    Loadrunner 基于webservice协议的接口压力测试(脚本实例) 接口功能如下:请求接口,报文只有一个参数为证件号码:返回报文中,有证件号码是否能查到对应数据,查到几条数据. 思路:请求w ...

  6. 10 逻辑完善以及bug修复

    进行到这里,我们应用开发已经接近尾声,我这里基本就是应用开发的记录过程,讲解的东西很少,有问题可以在评论区讨论呦.下面进入最后调整的阶段. 预览我们的应用,会发现首页的职位列表,也会显示收藏的星星图标 ...

  7. mysql工作原理(网络搜索整理的)

    原文网址:Mysql 工作原理 原文网址:MySQL运行原理与基础架构 mysql基本用法原文网址:MySQL(一):基本原理 SQL 语句执行过程 数据库通常不会被直接使用,而是由其他编程语言通过S ...

  8. 滑动窗体的最大值(STL的应用+剑指offer)

    滑动窗体的最大值 參与人数:767时间限制:1秒空间限制:32768K 通过比例:21.61% 最佳记录:0 ms|8552K(来自 ) 题目描写叙述 给定一个数组和滑动窗体的大小.找出全部滑动窗体里 ...

  9. 搭建nodejs服务,访问本地站点文件

    搭建nodejs服务器步骤: 1.安装nodejs服务(从官网下载安装) 2.在自己定义的目录下新建服务器文件如 server.js 例如,我在E:\PhpProject\html5\websocke ...

  10. 设计模式-(10)观察者模式 (swift版)

    一,概念 观察者(Observer)模式又名发布-订阅(Publish/Subscribe)模式.GOF给观察者模式如下定义:定义对象间的一种一对多的依赖关系,当一个对象的状态发生改变时,所有依赖于它 ...