Problem Description

Nowadays, we all know that Computer College is the biggest department in HDU. But, maybe you don't know that Computer College had ever been split into Computer College and Software College in 2002.
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

Input contains multiple test cases. Each test case starts with a number N (0 < N <= 50 -- the total number of different facilities). The next N lines contain an integer V (0<V<=50 --value of facility) and an integer M (0<M<=100 --corresponding number of the facilities) each. You can assume that all V are different.
A test case starting with a negative integer terminates input and this test case is not to be processed.

Output

For each case, print one line containing two integers A and B which denote the value of Computer College and Software College will get respectively. A and B should be as equal as possible. At the same time, you should guarantee that A is not less than B.

Sample Input

2
10 1
20 1
3
10 1
20 2
30 1
-1

Sample Output

20 10
40 40
解题思路:典型的多重背包问题,由于题目给的时限比较多,果断转化成01背包暴力过了233。题意是:尽量使得A,B两者分得的价值接近相等(两者价值总和为所有设施设备的总价值),并且A分得的价值不小于B分得的价值,那么问题就可以转化成不超过总价值的一半(视为背包的容量△(已经是确定值))下,挑选若干件物品装入背包后,使得总价值最大,即为B得到的最大价值,A得到的价值为sum-dp[sum/2]。最坏的时间复杂度大约为6e8级别,可见测试数据挺水的。
AC代码一(717ms):
 #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(多重背包)的更多相关文章

  1. 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 ...

  2. HDU 1171 Big Event in HDU (多重背包变形)

    Big Event in HDU Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  3. 组合数学 - 母函数的变形 --- hdu 1171:Big Event in HDU

    Big Event in HDU Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  4. HDU 1171 Big Event in HDU (多重背包)

    Big Event in HDU Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  5. hdu 1171 Big Event in HDU(母函数)

    链接:hdu 1171 题意:这题能够理解为n种物品,每种物品的价值和数量已知,现要将总物品分为A,B两部分, 使得A,B的价值尽可能相等,且A>=B,求A,B的价值分别为多少 分析:这题能够用 ...

  6. 【01背包】HDU 1171 Big Event in HDU

    Problem Description Nowadays, we all know that Computer College is the biggest department in HDU. Bu ...

  7. HDU 1171 Big Event in HDU dp背包

    Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s ...

  8. HDU 1171 Big Event in HDU 母函数

    欢迎参加——BestCoder周年纪念赛(高质量题目+多重奖励) Big Event in HDU Time Limit: 10000/5000 MS (Java/Others)    Memory ...

  9. 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 ...

随机推荐

  1. FTRL (Follow-the-regularized-Leader)算法

    Online gradient descent(OGD) produces excellent prediction accuracy with a minimum of computing reso ...

  2. [NPM] npm check to update the dependencies

    To update the dependencies in the project, we can run: npx npm-check -u

  3. POJ 1436 Horizontally Visible Segments(线段树)

    POJ 1436 Horizontally Visible Segments 题目链接 线段树处理染色问题,把线段排序.从左往右扫描处理出每一个线段能看到的右边的线段,然后利用bitset维护枚举两个 ...

  4. stl 之set图解

    使用set或multiset之前,必须增加头文件<set> Set.multiset都是集合类,区别在与set中不同意有反复元素,multiset中同意有反复元素. sets和multis ...

  5. Windows-安装composer

    安装laravel之前必须先安装componser,点击:下载Windows安装程序 全部下一步,直到完成 目前,遇到过两个问题(国内防火墙) 还有就是Win10不支持PHP+Composer的组合, ...

  6. Redis系列之-—Redis-cli命令总结【转】

    Redis-cli命令最新总结 参考资料: http://redisdoc.com/ 或者 http://doc.redisfans.com http://redis.io/commands 一. 进 ...

  7. URL编码总结

    URL编码总结           URL是Universal Resource Locator的简称.翻译过来那就是统一资源定位符,好吧,我们常常会俗称为网页地址. 一个URL的格式一般是这种:协议 ...

  8. 2.NetDh框架之简单高效的日志操作类(附源码和示例代码)

    前言 NetDh框架适用于C/S.B/S的服务端框架,可用于项目开发和学习.目前包含以下四个模块 1.数据库操作层封装Dapper,支持多种数据库类型.多库实例,简单强大: 此部分具体说明可参考博客: ...

  9. [Sciter] 资源引用

    http://www.cnblogs.com/yinxufeng/p/fb343eecda564aa63bce0bdf15709ddf.html 方式一. 加载外部文件方式二. 加载内存方式三. 加载 ...

  10. HQL语句详解

    4.3 使用HQL查询 Hibernate提供了异常强大的查询体系,使用Hibernate有多种查询方式.可以选择使用Hibernate的HQL查询,或者使用条件查询,甚至可以使用原生的SQL查询语句 ...