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. “约定优于配置”与Magento改造尝试四之block、helper和model载入

    暂定本章为这个系列最后一章,还是继续沿用模块的别名(alias)概念 <modules> <Mage_Wishlist> <version>1.6.0.0</ ...

  2. LoadRunner系列之—-03 用Java Vuser协议编写接口测试脚本

    待测试接口用java语言实现,且项目中调用该接口需要用专门的jar包.这种情况可以用Java Vuser协议实现接口调用脚本,类似java代码. 代码样例如下: /* * LoadRunner Jav ...

  3. 为什么java构造函数的构造器只能在第一行写this() 或者super() ?

    最近在看内部类, 但是被“为什么匿名内部类或者局部内部类使用方法的局部变量时, 局部变量一定得是final类型”困扰着, 在网上查找资料的时候, 发现我对类初始化完全不了解, 之前的认识都是错误! 所 ...

  4. 6 JobApp默认视图开发

    第一步:引入angularjs 添加app模块 现在我们正式进入开发,下面是我们在上一节建立的目录结构: 我们需要再src路径下,新建index.html文件,先引入angularjs文件: < ...

  5. 我的kindle书单

    刚刚入手kindle,希望能够持续阅读,不断进步. 列下书单,记录我的阅读足迹,更希望园友若有好书多多推荐,互相交流. # keep updating ... 我的kindle书单 book name ...

  6. 前台传JSON到后台

    现在,有一个需求,我需要将表格中选中行的数据中的一部分传直接传到控制器中,然后保存到另外一张表中.一开始,我就想到在前台使用ajax构造json数据,然后控制器直接通过list接收. 选中界面中的行, ...

  7. linux输入yum后提示: -bash: /usr/bin/yum: No such file or directory的解决方案

    linux输入yum后提示: -bash: /usr/bin/yum: No such file or directory的解决方案 今天在安装程序时,发现有一个插件未安装,我就随手敲了一个命令,看都 ...

  8. 浅谈UML的概念和模型之UML视图

    相信大家都知道UML的全称,统一建模语言(UML是 Unified Modeling Language的缩写)是用来对软件系统进行可视化建模的一种语言.UML为面向对象开发系统的产品进行说明.可视化. ...

  9. SAP 常用增强记录文档

    转自:http://blog.csdn.net/budaha 20170215需要一个PR 修改保存时候的增强,目的是同步PR的处理状态 EBAN-STATU 到一个自建表ZTPRTOPO,记得有个P ...

  10. centos7下tomcat7 或tomcat8启动超慢原因

    1,找到你的jdk安装的位置 ${JAVA_HOME}/jre/lib/security/java.security 2,vi 打开后找到 securerandom.source=file:/dev/ ...