多重背包(MultPack = ZeroOnePack + CompletePack)
HiHoCoder_offer6_04
题目4 : 奖券兑换
描述
小Hi在游乐园中获得了M张奖券,这些奖券可以用来兑换奖品。
可供兑换的奖品一共有N件。第i件奖品需要Wi张奖券才能兑换到,其价值是Pi。
小Hi使用不超过M张奖券所能兑换到的最大奖品总价值是多少?
输入
第一行两个整数N,M。
接下来N行,每行两个整数Wi,Pi。
对于 50%的数据: 1≤N,M≤1000
对于 100%的数据: 1≤N,M≤105,1≤Pi,Wi≤10。
输出
一行一个整数,表示最大的价值。
- 样例输入
-
3 10
2 3
8 8
10 10 - 样例输出
-
11
明显是一个01背包问题,背了一下结果:过了一半的数据,剩下的TLE了。 仔细观察数据, 1 <= n , m <= 100000, 1 <= wi , pi <= 10。明显每个物品的价值和容量都非常小,也就是说不同容量不同不价值的物品最多有:10 * 10 = 100种。
既然这个数据项这么小,那么它就是这个问题的突破口。对于每一个物品,已知它的num(数量 )和 weight(重量) & value(价值),刚好可以使用多重背包。加上二进制优化,复杂度O(10 * 10 * log 100000);
代码:
#include <iostream>
#include <cstdio>
#include <string>
using namespace std;
int num[][];
int dp[]; void CompletePack(int weight, int value, int totWeight)
{
for(int w = weight; w <= totWeight; w ++){
dp[w] = max(dp[w], dp[w - weight] + value);
}
} void zeroOnePack(int weight, int value, int totWeight)
{
for(int w = totWeight; w >= weight; w --){
dp[w] = max(dp[w], dp[w - weight] + value);
}
} void multiPack(int weight, int value, int totWeight)
{
if(weight * num[weight][value] > totWeight){
CompletePack(weight, value, totWeight);
}else{
int k = , tmpNum = num[weight][value];
while(k < tmpNum){
zeroOnePack(k * weight, k * value, totWeight);
tmpNum -= k;
k <<= ;
}
zeroOnePack(tmpNum * weight, tmpNum * value, totWeight);
}
} int main()
{
int n,m,weight,value;
cin>>n>>m;
for(int i = ; i < n; i ++){
scanf("%d%d",&weight, &value);
num[weight][value] ++;
}
for(int i = ; i <= ; i ++){
for(int j = ; j <= ; j ++){
multiPack(i, j, m);
}
}
printf("%d\n",dp[m]);
return ;
}
————————++++++————————
多重背包(MultPack = ZeroOnePack + CompletePack)的更多相关文章
- HDU-2844 Coins(多重背包)
Problem Description Whuacmers use coins.They have coins of value A1,A2,A3...An Silverland dollar. On ...
- hdu 2191 多重背包 悼念512汶川大地震遇难同胞——珍惜现在,感恩生活
http://acm.hdu.edu.cn/showproblem.php?pid=2191 New~ 欢迎“热爱编程”的高考少年——报考杭州电子科技大学计算机学院关于2015年杭电ACM暑期集训队的 ...
- DP大作战——多重背包
题目描述 在之前的上机中,零崎已经出过了01背包和完全背包,也介绍了使用-1初始化容量限定背包必须装满这种小技巧,接下来的背包问题相对有些难度,可以说是01背包和完全背包的进阶问题. 多重背包:物品可 ...
- poj 2392 多重背包
题意:有几个砖,给出高度,能放的最大高度和数目,求这些砖能垒成的最大高度 依据lim排个序,按一层一层进行背包 #include<cstdio> #include<iostream& ...
- poj 1276 多重背包
735 3 4 125 6 5 3 350 //735的最大额,3种,4个125,6个5,3个350 633 4 500 30 6 100 1 5 0 1 735 0 0 3 10 100 10 50 ...
- hdu 1059 多重背包
题意:价值分别为1,2,3,4,5,6的物品个数分别为a[1],a[2],a[3],a[4],a[5],a[6],问能不能分成两堆价值相等的. 解法:转化成多重背包 #include<stdio ...
- poj1276 多重背包
//Accepted 1100 KB 47 ms //多重背包 #include <cstdio> #include <cstring> #include <iostre ...
- poj2392 多重背包
//Accepted 868 KB 188 ms //多重背包 #include <cstdio> #include <cstring> #include <iostre ...
- poj1014 dp 多重背包
//Accepted 624 KB 16 ms //dp 背包 多重背包 #include <cstdio> #include <cstring> #include <i ...
随机推荐
- BZOJ 3648 寝室管理
[题解] GDOI2016 Day2T3 如果给出的数据是一棵树那么皆大欢喜直接点分治就好了,用树状数组维护大于x的数的个数.如果是一棵基环树,我们先断掉环上的一条边,然后跑点分治:再加上经过这条边的 ...
- 【codeforces 709D】Recover the String
[题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...
- Maven学习总结(29)——Maven项目的pom.xml中log4j2配置
<dependency> <groupId>org.apache.logging.log4j</groupId> <a ...
- Git学习总结(14)——Git使用前的注意事项
连接方式https.ssh 在使用git的时候,不管你的服务器是开源平台github还是私服gitlab,你都需要clone仓库到本地,这个clone的时候就需要你选择连接方式.这个连接方式决定了你与 ...
- UVA 12686 Trending Topic
Trending Topic Time limit: 1.000 seconds Imagine you are in the hiring process for a company whose p ...
- CodeForces 370C
这道题其实是挺简单的.首先很容易发现最多人用的颜色的人数如果大于n/2,就肯定不能让全部人都成功戴上两只不同颜色的手套.反过来想,如果这个人数小于等于n/2又如何呢?的确,这就能让全部人都能戴上两只不 ...
- Power of Matrix 等比数列求和 矩阵版!
#include<iostream> #include<cstdio> #include<cmath> #include<cstring> #inclu ...
- HDU1565 方格取数1(构图+网络流最大独立集合)
题目大意:给你一个n*n的格子的棋盘,每个格子里面有一个非负数. 从中取出若干个数,使得任意的两个数所在的格子没有公共边,就是说所取的数所在的2个格子不能相邻,并且取出的数的和最大. 解题思路:最大点 ...
- endWith is not a function
解决方法,增加String的扩展 String.prototype.endWith = function(suffix) { return this.indexOf(suffix, this.leng ...
- 【Python】Python 标准库 urllib2 的使用细节
转自:http://zhuoqiang.me/python-urllib2-usage.html http://www.cnblogs.com/txw1958/archive/2012/03/12/2 ...