题解报告: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 ...
随机推荐
- 主成分分析(principal components analysis)
http://www.cnblogs.com/jerrylead/tag/Machine%20Learning/ PCA的思想是将n维特征映射到k维上(k<n),这k维是全新的正交特征.这k维特 ...
- 配置Python 2.7.1外加环境pywin32-216.win32-py2.7
python-2.7.1 安装包 下载地址:http://download.csdn.net/detail/baidu_14854543/7985187 pywin32-216.win32-py2. ...
- eclipse环境下无法创建android virtual Devices(AVD)问题解决的方法汇总
首先,要在eclipse环境下成功的创建一个安卓虚拟机,须要有三项东西,第一就是eclipse,第二就是android SDK Manager,第三就是ADT,也就是eclipse环境下的一个安卓虚拟 ...
- 负载均衡之基于DNS负载
基于DNS的负载平衡 OK,在了解了负载平衡系统的大致组成及使用方式之后.我们就来看看各种负载解决方式. 当前业界中所最常使用的负载平衡解决方式主要分为三种:基于DNS的负载平衡,L3/4负载平衡,也 ...
- Activity调用isDestroyed()方法报出,java.lang.NoSuchMethodError
在測试App的过程中,Activity调用了isDestroyed()方法,报出了java.lang.NoSuchMethodError错误. 自己手机MI 2S,版本号4.1.1. 事实上原因就是i ...
- Windows-安装composer
安装laravel之前必须先安装componser,点击:下载Windows安装程序 全部下一步,直到完成 目前,遇到过两个问题(国内防火墙) 还有就是Win10不支持PHP+Composer的组合, ...
- hdu 3255 Farming(扫描线)
题目链接:hdu 3255 Farming 题目大意:给定N个矩形,M个植物,然后给定每一个植物的权值pi,pi表示种植物i的土地,单位面积能够收获pi,每一个矩形给定左下角和右上角点的坐标,以及s, ...
- C实现头插法和尾插法来构建单链表(不带头结点)
链表的构建事实上也就是不断插入节点的过程.而节点的插入能够分为头插法和尾插法. 头插法就是在头结点后插入该节点,始终把该节点作为第一个节点.尾插法就是在链表的最后一个节点处插入元素,作为最后一个节点. ...
- hdu1576 mod 运算的逆元
Problem Description 要求(A/B)%9973,但因为A非常大,我们仅仅给出n(n=A%9973)(我们给定的A必能被B整除.且gcd(B,9973) = 1). Input 数 ...
- eclispe pydev tab改回 空格找到方法了,这个链接:http://stackoverflow.com/questions/23570925/eclipse-indents-new-line-with-tabs-instead-of-spaces
看这个链接: 3down votefavorite 1 I've followed all the suggestions here. When I press return, I get a new ...