(01背包 第k优解) Bone Collector II(hdu 2639)
Here is the link:http://acm.hdu.edu.cn/showproblem.php?pid=2602
Today we are not desiring the maximum value of bones,but the K-th maximum value of the bones.NOTICE that,we considerate two ways that get the same value of bones are the same.That means,it will be a strictly decreasing sequence from the 1st maximum , 2nd maximum .. to the K-th maximum.
If the total number of different values is less than K,just ouput 0.
Followed by T cases , each case three lines , the first line contain two integer N , V, K(N <= 100 , V <= 1000 , K <= 30)representing the number of bones and the volume of his bag and the K we need. And the second line contain N integers representing the value of each bone. The third line contain N integers representing the volume of each bone.
5 10 2
1 2 3 4 5
5 4 3 2 1
5 10 12
1 2 3 4 5
5 4 3 2 1
5 10 16
1 2 3 4 5
5 4 3 2 1
2
0
- #include <iostream>
- #include <cstdio>
- #include <cstring>
- #include <queue>
- #include <vector>
- #include <map>
- #include <algorithm>
- using namespace std;
- const int N = ;
- const int INF = 0x3fffffff;
- const long long MOD = ;
- typedef long long LL;
- #define met(a,b) (memset(a,b,sizeof(a)))
- int dp[N][];
- int a[N], b[N], c[N];
- ///dp[j][k] 代表容量为 j 的背包的第 k+1 优解
- int cmp(int a, int b)
- {
- return a > b;
- }
- int main()
- {
- int T;
- scanf("%d", &T);
- while(T--)
- {
- int i, j, k, n, v;
- scanf("%d%d%d", &n, &v, &k);
- met(a, );
- met(b, );
- met(dp, );
- for(i=; i<=n; i++)
- scanf("%d", &a[i]);
- for(i=; i<=n; i++)
- scanf("%d", &b[i]);
- for(i=; i<=n; i++)
- {
- for(j=v; j>=b[i]; j--)
- {
- int w = ;
- for(int z=; z<k; z++) ///每次只需考虑前 k 优解的状态转换即可
- {
- c[w++] = dp[j][z];
- c[w++] = dp[j-b[i]][z]+a[i];
- }
- sort(c, c+w, cmp);
- w = unique(c, c+w) - c;
- for(int t=; t<k && t<w; t++) ///t的范围, 既不能大于 k,也不能大于 w
- dp[j][t] = c[t];
- }
- }
- printf("%d\n", dp[v][k-]);
- }
- return ;
- }
(01背包 第k优解) Bone Collector II(hdu 2639)的更多相关文章
- 01背包之求第K优解——Bone Collector II
http://acm.hdu.edu.cn/showproblem.php?pid=2639 题目大意是,往背包里赛骨头,求第K优解,在普通01背包的基础上,增加一维空间,那么F[i,v,k]可以理解 ...
- HDU 2639 (01背包第k优解)
/* 01背包第k优解问题 f[i][j][k] 前i个物品体积为j的第k优解 对于每次的ij状态 记下之前的两种状态 i-1 j-w[i] (选i) i-1 j (不选i) 分别k个 然后归并排序并 ...
- HDU 3639 Bone Collector II(01背包第K优解)
Bone Collector II Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- 杭电 2639 Bone Collector II【01背包第k优解】
解题思路:对于01背包的状态转移方程式f[v]=max(f[v],f[v-c[i]+w[i]]);其实01背包记录了每一个装法的背包值,但是在01背包中我们通常求的是最优解, 即为取的是f[v],f[ ...
- Bone Collector II HDU - 2639 01背包第k最大值
题意: 01背包,找出第k最优解 题解: 对于01背包最优解我们肯定都很熟悉 第k最优解的话也就是在dp方程上加一个维度来存它的第k最优解(dp[i][j]代表,体积为i能获得的第j最大价值) 对于每 ...
- 01背包-第k优解
The title of this problem is familiar,isn't it?yeah,if you had took part in the "Rookie Cup&quo ...
- hdu2639 01背包第K优解
#include<iostream> #include<cstdio> #include<algorithm> #include<cstring> #i ...
- HDU2639Bone Collector II[01背包第k优值]
Bone Collector II Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- HDU 2639 背包第k优解
Bone Collector II Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
随机推荐
- thinkphp装修平台源码
每日签到微擎微赞自助授权中心站长工具(new)☜=每日新帖=微信开发手册VIP优惠活动 开启辅助访问切换到宽版 用户名 自动登录 找回密码 密码 登录 立即注册 只需一步,快速开始 首页 微鱼商业 ...
- angular 1.x 控制器之间互相传递参数
我们要向前方看齐,基于js引用类型的对象就不记了,所以使用基于事件的方式: angular 中 $on,$emit,$boardcast来实现父控制器和子控制器互相通讯, 其中$on表示事件监听, $ ...
- 多进程copy文件
from multiprocessing import Pool,Manager import os,time def copyFileTask(fileName,oldFolderName,newF ...
- Linux系统性能监控工具介绍之-tsar
Linux系统性能监控工具介绍之-tsar Linux系统性能监控工具介绍之-tsar 2017-03-02 20:25 175人阅读 评论(0) 收藏 举报 分类: LINUX调优(9) 目 ...
- Real-time qPCR So Easy?
Real-time qPCR So Easy? [2016-05-27] 实时荧光定量PCR技术是在定性RCR技术基础上发展起来的核酸定量技术,于1996年由美国Applied biosy ...
- BZOJ2330或洛谷3275 [SCOI2011]糖果
BZOJ原题链接 洛谷原题链接 很明显的差分约束,但数据范围较大,朴素\(SPFA\)判正环求解会\(T\)(理论上如此,但我看到有挺多人用朴素的还跑得挺快..),所以需要优化. 我们所建立的有向图中 ...
- 1.3 Java中的标识符和关键字
1.标识符 含义:标识符用于给程序中的类.变量.方法命名的符号. 组成:数字(0-9).字母(a-z)(A-Z).下划线(_).美元符号$. 命名规则:1.数字不能够作为命名符号的开头 2.不能够使用 ...
- Spring整合jedis 集群模式
引入jedis依赖 <dependency> <groupId>redis.clients</groupId> <artifactId>jedis< ...
- 视频基础知识:浅谈视频会议中H.264编码标准的技术发展
浅谈视频会议中H.264编码标准的技术发展 浅谈视频会议中H.264编码标准的技术发展 数字视频技术广泛应用于通信.计算机.广播电视等领域,带来了会议电视.可视电话及数字电视.媒体存储等一系列应用,促 ...
- 进入快速通道的委托(深入理解c#)
1.方法组:所有的名称相同的重载方法合在一起就成为一个方法组. 2.协变性和逆变性: 协变性指的是——泛型类型参数可以从一个派生类隐式转化为基类. 逆变性指的是——泛型类型参数可以从一个基类隐式转化为 ...