题解报告:hdu 1171 Big Event in HDU(多重背包)
Problem Description
The splitting is absolutely a big event in HDU! At the same time, it is a trouble thing too. All facilities must go halves. First, all facilities are assessed, and two facilities are thought to be same if they have the same value. It is assumed that there is N (0<N<1000) kinds of facilities (different value, different kinds).
Input
A test case starting with a negative integer terminates input and this test case is not to be processed.
Output
Sample Input
Sample Output
#include<bits/stdc++.h>
using namespace std;
int n,v,m,num,sum,vi[],dp[];//最大价值的一半:50*100*50/2=125000,数组开大一点即可
int main(){
while(~scanf("%d",&n)&&n>=){
memset(vi,,sizeof(vi));
memset(dp,,sizeof(dp));
num=sum=;//sum记录总价,num记录物品的总数
while(n--){
scanf("%d%d",&v,&m);
while(m--){//有m个价值相等的物品
vi[num++]=v;//把m个物品看成m件不同的物品,多重背包转化成01背包
sum+=v;//累加价值
}
}
for(int i=;i<num;++i)
for(int j=sum/;j>=vi[i];--j)
dp[j]=max(dp[j],dp[j-vi[i]]+vi[i]);
printf("%d %d\n",sum-dp[sum/],dp[sum/]);//dp[sum/2]比较小即为B,因为求解过程是让max尽量靠近sum/2
}
return ;
}
AC代码二(78ms):
#include<bits/stdc++.h>
using namespace std;
int t,W,n,tol,value[],num[],dp[];
void ZeroOnePack(int w,int v){//01背包
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",&n)&&n>=){
memset(dp,,sizeof(dp));tol=W=;
for(int i=;i<=n;++i){
scanf("%d%d",&value[i],&num[i]);
tol+=value[i]*num[i];
}
W=tol/;
for(int i=;i<=n;++i)
MultiplePack(value[i],value[i],num[i]);
printf("%d %d\n",tol-dp[W],dp[W]);
}
return ;
}
AC代码三(62ms):单调队列优化
#include<bits/stdc++.h>
using namespace std;
int W,n,tol,val[],num[],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",&n)&&n>=){
tol=W=;memset(dp,,sizeof(dp));
for(int i=;i<=n;++i)
scanf("%d%d",&val[i],&num[i]),tol+=val[i]*num[i];
W=tol/;
for(int i=;i<=n;i++){
num[i]=min(num[i],W/val[i]);
SingleDeque(val[i],val[i],num[i]);
}
printf("%d %d\n",tol-dp[W],dp[W]);
}
return ;
}
题解报告:hdu 1171 Big Event in HDU(多重背包)的更多相关文章
- HDU 1171 Big Event in HDU 多重背包二进制优化
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1171 Big Event in HDU Time Limit: 10000/5000 MS (Jav ...
- HDU 1171 Big Event in HDU (多重背包变形)
Big Event in HDU Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- 组合数学 - 母函数的变形 --- hdu 1171:Big Event in HDU
Big Event in HDU Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- HDU 1171 Big Event in HDU (多重背包)
Big Event in HDU Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- hdu 1171 Big Event in HDU(母函数)
链接:hdu 1171 题意:这题能够理解为n种物品,每种物品的价值和数量已知,现要将总物品分为A,B两部分, 使得A,B的价值尽可能相等,且A>=B,求A,B的价值分别为多少 分析:这题能够用 ...
- 【01背包】HDU 1171 Big Event in HDU
Problem Description Nowadays, we all know that Computer College is the biggest department in HDU. Bu ...
- HDU 1171 Big Event in HDU dp背包
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s ...
- HDU 1171 Big Event in HDU 母函数
欢迎参加——BestCoder周年纪念赛(高质量题目+多重奖励) Big Event in HDU Time Limit: 10000/5000 MS (Java/Others) Memory ...
- HDU 1171 Big Event in HDU【01背包/求两堆数分别求和以后的差最小】
Big Event in HDU Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) T ...
随机推荐
- zedboard硬件连接过程
1. ZedBoard – Connect a 2nd micro-USBcable between the host machine and connector J17 (JTAG) 2. ...
- [Angular] Write Compound Components with Angular’s ContentChild
Allow the user to control the view of the toggle component. Break the toggle component up into multi ...
- Win7 本地打印后台处理程序服务没有运 怎么办
找到名为Print Spooler的服务,启动类型改为自动,服务状态改为启动即可.
- Lync 2013 与Exchange 2013 UM&UC 集成!
设置好对应的拨号计划.我们设置分机号码为4位: 配置好接入号码为5000: 配置自己主动助理号码为6000: 改动UM拨号模式为双模式: Set-UMService -identity Exch ...
- android 多进程 Binder AIDL Service
本文參考http://blog.csdn.net/saintswordsman/article/details/5130947 android的多进程是通过Binder来实现的,一个类,继承了Bind ...
- 【Mongodb教程 第一课 补加课】 Failed to connect to 127.0.0.1:27017, reason: errno:10061 由于目标计算机积极拒绝,无法连接
1:启动MongoDB 2014-07-25T11:00:48.634+0800 warning: Failed to connect to 127.0.0.1:27017, reason: errn ...
- JS应用之禁止抓屏、复制、打印
JS应用之禁止抓屏.复制.打印项目需要禁止抓屏.复制.打印的要求,复制.打印做起来可能顺手一点网上各种各样的脚本俯首皆是.但抓屏怎么禁止?PrintScreen是一个特殊的键,它是没有keyCode的 ...
- 安装SQLserver2008时出现的错误
1.SQLserver2008提示必须重新启动计算机才干够继续安装.解决方法例如以下: 在開始->执行中输入regedit,到HKEY_LOCAL_MACHINE\SYSTEM\CurrentC ...
- 2016/3/26 weixin 头像 昵称 网页优化显示 缺表中数据 只有代码 无显示效果
weixin.php <!DOCTYPE html> <html lang="en"> <head> <meta charset=&quo ...
- 2016/3/16 45道MySQL 查询练习题
一. 设有一数据库,包括四个表:学生表(Student).课程表(Course).成绩表(Score)以及教师信息表(Teacher).四个表的结构分别如表1-1的表(一)~表( ...