题解报告:hdu 1059 Dividing(多重背包、多重部分和问题)
Problem Description
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
The last line of the input file will be "0 0 0 0 0 0''; do not process this line.
Output
Output a blank line after each test case.
Sample Input
Sample Output
#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(多重背包、多重部分和问题)的更多相关文章
- HDU 1059 Dividing 分配(多重背包,母函数)
题意: 两个人共同收藏了一些石头,现在要分道扬镳,得分资产了,石头具有不同的收藏价值,分别为1.2.3.4.5.6共6个价钱.问:是否能公平分配? 输入: 每行为一个测试例子,每行包括6个数字,分别对 ...
- ACM学习历程—HDU 1059 Dividing(dp && 多重背包)
Description Marsha and Bill own a collection of marbles. They want to split the collection among the ...
- HDOJ(HDU).1059 Dividing(DP 多重背包+二进制优化)
HDOJ(HDU).1059 Dividing(DP 多重背包+二进制优化) 题意分析 给出一系列的石头的数量,然后问石头能否被平分成为价值相等的2份.首先可以确定的是如果石头的价值总和为奇数的话,那 ...
- hdu 1059 Dividing bitset 多重背包
bitset做法 #include <bits/stdc++.h> #define PI acos(-1.0) #define mem(a,b) memset((a),b,sizeof(a ...
- hdu 1059 Dividing(多重背包优化)
Dividing Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Su ...
- 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 ...
- hdu 1059 Dividing
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission( ...
- HDU 1059 Dividing (dp)
题目链接 Problem Description Marsha and Bill own a collection of marbles. They want to split the collect ...
- hdu 1059 Dividing 多重背包
点击打开链接链接 Dividing Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
随机推荐
- Android之怎样实现滑动页面切换【Fragment】
Fragment 页面切换不能滑动 所以对于listview 能够加入的左右滑动事件 .不会有冲突比如(QQ的好友列表的删除) Fragment 和viewpager 的差别 Viewpager ...
- 【LeetCode-面试算法经典-Java实现】【066-Plus One(加一)】
[066-Plus One(加一)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a non-negative number represented as ...
- nginx内存池
一.设计原则 (1)降低内存碎片 (2)降低向操作系统申请内存的次数 (3)减少各个模块的开发效率 二.源代码结构 struct ngx_pool_s { ngx_pool_data_t ...
- 淘宝API学习之道:淘宝TOP之API接口接入教程
作为一个中小型站点开发人员,淘宝API的开放大大缩短了站点的开发周期和运作效率.面对海量的数据.开发人员仅仅要细致阅读开发文档,熟悉对应的接口,就能够把数据导入自己的站点,这样就不必望洋兴叹了. 眼下 ...
- 如何将Python的py程序打包成跨平台的exe文件
在编写了自己的第一个可以爬写网页源代码的程序之后,发现如果在没有安装了pythonLDLE程序的计算机上根本就跑不出来.所以开始寻找可以将程序打包成跨平台运行的exe文件. 经过自己费力的谷歌没有一个 ...
- Python爬虫开发【第1篇】【动态HTML、Selenium、PhantomJS】
JavaScript JavaScript 是网络上最常用也是支持者最多的客户端脚本语言.它可以收集用户的跟踪数据,不需要重载页面直接提交表单,在页面嵌入多媒体文件,甚至运行网页游戏. 我们可以在网页 ...
- 浅谈JavaScript的面向对象程序设计(三)
前面已经对JavaScript的面向对象程序设计作了简单的介绍,包括了对象的属性.对象的工厂模式.构造函数和原型等.通过介绍,这些创建对象的方法依然有不少优化和改进的地方. 组合使用构造函数模式和原型 ...
- Win32 Windows编程 七
定时器消息 1. WM_TIMER 依照定时器设置的时间段,自己主动向窗体发送一个定时器消息WM_TIMER.优先级比較低 定时器精度比較低.毫秒级别.消息产生时间也精度比較低 2 .消息和函数 WM ...
- 2016/4/1 jquery 与javascript关系 ①取元素 ②操作内容 ③操作属性 ④操作 样式 ⑤ 事件 点击变色
jQuery的min版本和原版功能是一样的,min版主要应用于已经开发成的网页中,而非min版 的文件比较大,里面有整洁的代码书写规范和注释,主要应用于脚本开发过程当中. JQuery是继protot ...
- Linux常用服务安装部署
1,centos7默认是装有python的,检查python版本的命令 # 检查python版本 : python -V 2,centOS在安装python3以及tab补全功能 下载python3源码 ...