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. IOS开发 Block的学习

    苹果公司正在大力推广Block块语法的使用,据说Block会迟早取代一般协议代理的使用. Block最大的作用是函数回调,简化代码. 在ios中,将blocks当成对象来处理,它封装了一段代码,这段代 ...

  2. [Angular] Modify User Provided UI with Angular Content Directives

    If we’re going to make our toggle accessible, we’ll need to apply certain aria attributes to the con ...

  3. Linux VPS/server上用Crontab来实现VPS自己主动化

    VPS或者server上常常会须要VPS或者server上常常会须要定时备份数据.定时运行重新启动某个服务或定时运行某个程序等等,一般在Linux使用Crontab,Windows以下是用计划任务(W ...

  4. HDU 2795 Billboard(宣传栏贴公告,线段树应用)

    HDU 2795 Billboard(宣传栏贴公告,线段树应用) ACM 题目地址:HDU 2795 Billboard 题意:  要在h*w宣传栏上贴公告,每条公告的高度都是为1的,并且每条公告都要 ...

  5. 各种“GND”

    资料来自网上,把个人觉得靠谱的摘取下来 1.地分类: a)直流地:直流电路“地”,零电位参考点: b)交流地:交流电的零线.要与地线区别开,不过,有时候拉电入户之前会把地线和零线接在一起: c)功率地 ...

  6. STM32的精确延时

    /*---------------------------------------------------------- 文件名:systick.c 文件描写叙述:sysTick 系统滴答时钟1us中 ...

  7. 反爬统计 数据库 sql CASE

    -- 经排查日志,发现ordertest.com下的url检测,频繁<Response [403]>,Forbidden;再进一步查询数据库数据:逐日统计错误临时表test_error_t ...

  8. USING REFLECTION

    In this section, you take a closer look at the System.Type class, which enables you to access inform ...

  9. CentOS 7.2 源码安装Python3.6

    1.环境 安装CentOS 7.2最小系统(CentOS-7-x86_64-Minimal-1511.iso) 2.需求 Python-3.6.4.tar.xz(官网下载) GCC(yum安装) 一堆 ...

  10. UVA10462Is There A Second Way Left? —— 次小生成树 kruskal算法

    题目链接:https://vjudge.net/problem/UVA-10462 Nasa, being the most talented programmer of his time, can’ ...