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. 排查程序死循环,死锁的方法 ——pstack

    pstack命令可显示每个进程的栈跟踪,pstack $pid即可,pstack命令须由$pid进程的属主或者root运行. 这次出现cpu占比100%的情况,但看memory占比,并无异常,怀疑是某 ...

  2. DNS查询的工作原理

    二.DNS查询的工作原理 1.DNS查询过程按两部分进行     1.名称查询从客户端计算机开始, 并传送给本机的DNS客户服务程序进行解析     2.如果不能再本机解析查询, 可根据设定的查询DN ...

  3. 视频云SDK iOS持续集成项目实践

    1. 前言 2016年, 我们维护的 iOS推流播放融合SDK KSYLive_iOS 在github上发布了40多个版本, 平均两周发布一个新版本, 经历了最初痛苦的全手动版本构建和维护, 到后来慢 ...

  4. mysql超时机制

    mysql每次建立一个socket连接(connect)时,这个socket都会占用一定内存.即使你关闭(close)连接时,并不是真正的关闭,而是处于睡眠(sleep)状态. 当你下次再进行连接时, ...

  5. Linux下jira自启动设置

    jira 的启动主要依靠的是bin目录下的catalina.sh脚本,提供了如init脚本的start,stop等参数----------------------------------------- ...

  6. vue2.0父子组件以及非父子组件如何通信

    1.父组件传递数据给子组件 父组件数据如何传递给子组件呢?可以通过props属性来实现 父组件: <parent> <child :child-msg="msg" ...

  7. springBoot系列教程07:异常捕获

    发生异常是很正常的事,异常种类也是千奇百怪,发生异常并不可怕,只要正确的处理,并正确的返回错误信息并无大碍,如果不进行捕获或者处理,分分钟服务器宕机是很正常的事 所以处理异常时,最基本的要求就是发生异 ...

  8. JAVA类的创建: 创建JAVA的类 ,JAVA的字段,JAVA类的方法

    1. 创建Java的类 如果说Java的一切都是对象,那么类型就是决定了某一类对象的外观与行为.可是类型的关键字不是type,而是class,创建一个新的类型要用下面的代码: 1 2 3 class ...

  9. 浅谈MVC MVP MVVM

    复杂的软件必须有清晰合理的架构,否则无法开发和维护. MVC(Model-View-Controller)是最常见的软件架构之一,业界有着广泛应用. 它本身很容易理解,但是要讲清楚,它与衍生的 MVP ...

  10. flask-form用户登录与注册

    用户登录 #!/usr/bin/env python # -*- coding:utf- -*- from flask import Flask, render_template, request, ...