http://acm.hdu.edu.cn/showproblem.php?pid=1171

Big Event in HDU

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

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
 

题意:这个题是一个多重背包,b的最大容量为总价值/2;

网上提供的有用母函数或者是纯暴力的方式,将每一种可达状态标记成1,最后找最接近最大容量的值,例如第二个案例 可以只找小于最大容量的可能达到的状态,及10 20 30 40

这种思路应用到背包上面就是将多重背包转化成0 1背包,

•第i件物品可以分为q[i]件物品,价值分别为w[i], 2*w[i], 3*w[i], ……,费用分别为c[i], 2*c[i], 3*c[i], ……。

即把k个物品i合成一个物品

优化

•把k个物品i合成一个物品
•只保留k= 1, 2, 4, …, 2^(p-1), q[i]-2^p+1的物品
•其中p为满足q[i]-2^p+1>0的最大整数
•这样可以保证这些物品的组合能够取到从1到q[i]的所有值且肯定不会超过q[i]
但是多重背包有一个更快的模板
 #include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
#define N 55 int val;
int f[N*]; //每件物品只能使用一次
void onezeropack(int v,int c)
{
int j;
for(j=val; j>=v; j--)
{
f[j]=max(f[j-v]+c,f[j]);
}
}
//每件物品可以无限使用
void completepack(int v,int c)
{
int j;
for(j=v; j<=val; j++)
{
f[j]=max(f[j-v]+c,f[j]);
}
}
//每件物品有限次使用
void multiplepack(int v,int c,int num)
{
if(v*num>=val)
{
completepack(v,c);
return;
}
int k=;
while(k<num)
{
onezeropack(k*v,k*c);
num=num-k;
k=k*;
}
onezeropack(num*v,num*c);
} int v[N], num[N];
int main()
{
int n;
while(~scanf("%d", &n), n >= )
{
for(int i = ; i < n; i++) scanf("%d %d", &v[i], &num[i]);
val = ;
for(int i = ; i < n; i++) val += v[i]*num[i];
int tm = val;
val /= ;
memset(f, , sizeof(f));
for(int i = ; i < n; i++) multiplepack(v[i], v[i], num[i]);
printf("%d %d\n", tm-f[val], f[val]);
}
return ;
}

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 多重背包

    B - Big Event in HDU Nowadays, we all know that Computer College is the biggest department in HDU. B ...

  3. hdu1171 Big Event in HDU(多重背包)

    http://acm.hdu.edu.cn/showproblem.php?pid=1171 多重背包题目不难,但是有些点不能漏或错. #include<iostream> #includ ...

  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. HDU1171:Big Event in HDU(多重背包分析)

    通过分析,要使A>=B并且差值最小.所以只要使sum/2的容量下,B最大就Ok了 #include<iostream> #include<cstdio> #include ...

  6. HUD 1171 Big Event in HDU(01背包)

    Big Event in HDU Problem Description Nowadays, we all know that Computer College is the biggest depa ...

  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. hdu1171Big Event in HDU(01背包)

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

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

随机推荐

  1. 创建一个可用的简单的SpringMVC项目,图文并茂

    转载麻烦注明下来源:http://www.cnblogs.com/silentdoer/articles/7134332.html,谢谢. 最近在自学SpringMVC,百度了很多资料都是比较老的,而 ...

  2. 动态WebApi

    动态WebApi实现了直接对Service的调用,其实没有跨过ApiController,只是我们自己创建出ApiController 实现主要分以下几步 一 对默认WebApi服务的替换 ApiGl ...

  3. VS2012 未找到与约束ContractName Microsoft.VisualStudio.Text.ITextDocumentFactoryService

    最近新换了系统还真是问题多多呀!! 系统更新补丁后打开 VS2012 ,新建C#项目的时候出现这个问题 VS2012 未找到与约束ContractName Microsoft.VisualStudio ...

  4. windows日志监控

    bat脚本,主要作用,每个五分钟读取日文本件中新增内容,进行错误赛选,如果有错误信息,将错误信息用邮件发送给管理员. 其中awk和sed需要手动下载 :读取number.txt文档,获取上一次执行时文 ...

  5. jmeter中一次运行多条sql语句

    操作比较简单,主要就分两步: 第一步:在JDBC Connection Configuration中设置,主要见下图标注部分增加:?allowMultiQueries=true 第二步:在JDBC R ...

  6. BeanShell断言(一)

    在beanShell中直接可以调用的变量,无需加前缀. 1.log 打印日志 log.info(“在控制台打印日志”); 2.SampleResult 获取SampleResult对象,可以通过这个对 ...

  7. selenium 封装

    周末无聊 在家封装一个pyselenium.可能这些封装大家都会使用,但是我还是根据我自己的习惯去选择性的去封装一些在我工作中用的,这样的话,我就不用去看selenium的api的,我可以根据我自己的 ...

  8. python导入模块时的执行顺序

    当python导入模块,执行import语句时,到底进行了什么操作?按照python的文档,她执行了如下的操作: 第一步,创建一个新的module对象(它可能包含多个module) 第二步,把这个mo ...

  9. Python函数返回不定数量的值

    Python的函数是可以return多个值的,但其本质上还是返回单个值,只是利用了tuple的自动打包,将多个值打包成单个tuple返回. 使用代码验证: def func_a(): return 1 ...

  10. 对vuex的认识和简单理解

    vuex概述 Vuex 是一个主要应用在中大型单页应用的类似于 Flux 的数据管理架构.它主要帮我们更好地组织代码,以及把应用内的的状态保持在可维护.可理解的状态. 但如果是简单的应用 ,就没有必要 ...