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. 历年NOIP选题题解汇总

    联赛前上vijos板刷往年联赛题,使用在线编辑编写代码,祝我rp++. 废话不多说,挑比较有意思的记一下. 题目是按照年份排序的,最早只到了03年. 有些题目因为 我还没写/很早之前写的忘了 所以就没 ...

  2. 9.11 test

    题面.pdf T1:通过打表发现,从一个点出发只有距离为1,2,3,5,6,9,10,13,17的点才不能到达: 那么我们转移的时候只有距离在20以内才不一定能直接转移,那么我们离散化之后; 对于每一 ...

  3. java 学习(二)

    public class Scoure { public static void main(String args[]) { int score=90; if (score>=85 && ...

  4. 关于java字节流的read()方法返回值为int的思考

    我们都知道java中io操作分为字节流和字符流,对于字节流,顾名思义是按字节的方式读取数据,所以我们常用字节流来读取二进制流(如图片,音乐 等文件).问题是为什么字节流中定义的read()方法返回值为 ...

  5. python2 与python3的变化

    1 写文件如果是bytes类型的话,打开文件 open参数设置为wb 2 python2 默认包import是相对路径,python3是绝对路径 3 python3的dict没有has_key方法,用 ...

  6. K:java中properties文件的读写

    Properties类与.properties文件:   Properties类继承自Hashtable类并且实现了Map接口,也是使用一种键值对的形式来保存属性集的类,不过Properties有特殊 ...

  7. Asp,NET控制文件上传的大小

    在web.config中的system.web 节点下添加如下代码: 第2行的maxRequestLength="8192",这里限制最大为8MB,可以自行设置.execution ...

  8. CSS3 自定义动画(animation)

    除了在之前的文章中介绍过的 CSS3 的变形 (transformation) 和转换 (transition) 外,CSS3 还有一种自由度更大的自定义动画,开发者甚至可以使用变形(transfor ...

  9. 一起学习Hibernate: Hibernate01 —— Hibernate的概述与入门案例

    一 Hibernate的介绍 1 让我们从JDBC与替代它的框架Hibernate进行一下对比. 1.1 JDBC的缺点 1) 代码结构繁琐.每次书写sql语句操作数据库都得需要很多步; 2) 是面向 ...

  10. opensuse安装pycurl失败记录

    早上在opensuse安装pycurl,一直出现如下错误: pepper@VM_56_243_suse:~/code/gitosis-autotest> pip install pycurl C ...