Problem Description
The title of this problem is familiar,isn't it?yeah,if you had took part in the "Rookie Cup" competition,you must have seem this title.If you haven't seen it before,it doesn't matter,I will give you a link:

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.

 
Input
The first line contain a integer T , the number of cases.
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.
 
Output
One integer per line representing the K-th maximum of the total value (this number will be less than 231).
 
Sample Input
3
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
 
Sample Output
12
2
0
 
思路:
对于求最优解的情况,我们对每一种状态只保存了该状态下的最优解,忽略了其他解,进而实现状态之间的转移,而对于求第K优解的情况呢?其实只需要保存每一种状态下的前K优解,从这K个状态进行状态间的转移,同时去重,保存当前状态的K优解即可。(感觉时间复杂度还是挺高的)
 
 
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <queue>
  5. #include <vector>
  6. #include <map>
  7. #include <algorithm>
  8. using namespace std;
  9.  
  10. const int N = ;
  11. const int INF = 0x3fffffff;
  12. const long long MOD = ;
  13. typedef long long LL;
  14. #define met(a,b) (memset(a,b,sizeof(a)))
  15.  
  16. int dp[N][];
  17. int a[N], b[N], c[N];
  18. ///dp[j][k] 代表容量为 j 的背包的第 k+1 优解
  19.  
  20. int cmp(int a, int b)
  21. {
  22. return a > b;
  23. }
  24.  
  25. int main()
  26. {
  27. int T;
  28. scanf("%d", &T);
  29. while(T--)
  30. {
  31. int i, j, k, n, v;
  32.  
  33. scanf("%d%d%d", &n, &v, &k);
  34.  
  35. met(a, );
  36. met(b, );
  37. met(dp, );
  38.  
  39. for(i=; i<=n; i++)
  40. scanf("%d", &a[i]);
  41. for(i=; i<=n; i++)
  42. scanf("%d", &b[i]);
  43.  
  44. for(i=; i<=n; i++)
  45. {
  46. for(j=v; j>=b[i]; j--)
  47. {
  48. int w = ;
  49. for(int z=; z<k; z++) ///每次只需考虑前 k 优解的状态转换即可
  50. {
  51. c[w++] = dp[j][z];
  52. c[w++] = dp[j-b[i]][z]+a[i];
  53. }
  54.  
  55. sort(c, c+w, cmp);
  56. w = unique(c, c+w) - c;
  57. for(int t=; t<k && t<w; t++) ///t的范围, 既不能大于 k,也不能大于 w
  58. dp[j][t] = c[t];
  59. }
  60. }
  61.  
  62. printf("%d\n", dp[v][k-]);
  63.  
  64. }
  65. return ;
  66. }

(01背包 第k优解) Bone Collector II(hdu 2639)的更多相关文章

  1. 01背包之求第K优解——Bone Collector II

    http://acm.hdu.edu.cn/showproblem.php?pid=2639 题目大意是,往背包里赛骨头,求第K优解,在普通01背包的基础上,增加一维空间,那么F[i,v,k]可以理解 ...

  2. 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个 然后归并排序并 ...

  3. HDU 3639 Bone Collector II(01背包第K优解)

    Bone Collector II Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  4. 杭电 2639 Bone Collector II【01背包第k优解】

    解题思路:对于01背包的状态转移方程式f[v]=max(f[v],f[v-c[i]+w[i]]);其实01背包记录了每一个装法的背包值,但是在01背包中我们通常求的是最优解, 即为取的是f[v],f[ ...

  5. Bone Collector II HDU - 2639 01背包第k最大值

    题意: 01背包,找出第k最优解 题解: 对于01背包最优解我们肯定都很熟悉 第k最优解的话也就是在dp方程上加一个维度来存它的第k最优解(dp[i][j]代表,体积为i能获得的第j最大价值) 对于每 ...

  6. 01背包-第k优解

    The title of this problem is familiar,isn't it?yeah,if you had took part in the "Rookie Cup&quo ...

  7. hdu2639 01背包第K优解

    #include<iostream> #include<cstdio> #include<algorithm> #include<cstring> #i ...

  8. HDU2639Bone Collector II[01背包第k优值]

    Bone Collector II Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  9. HDU 2639 背包第k优解

    Bone Collector II Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

随机推荐

  1. thinkphp装修平台源码

    每日签到微擎微赞自助授权中心站长工具(new)☜=每日新帖=微信开发手册VIP优惠活动 开启辅助访问切换到宽版 用户名 自动登录  找回密码 密码 登录  立即注册 只需一步,快速开始 首页 微鱼商业 ...

  2. angular 1.x 控制器之间互相传递参数

    我们要向前方看齐,基于js引用类型的对象就不记了,所以使用基于事件的方式: angular 中 $on,$emit,$boardcast来实现父控制器和子控制器互相通讯, 其中$on表示事件监听, $ ...

  3. 多进程copy文件

    from multiprocessing import Pool,Manager import os,time def copyFileTask(fileName,oldFolderName,newF ...

  4. Linux系统性能监控工具介绍之-tsar

    Linux系统性能监控工具介绍之-tsar Linux系统性能监控工具介绍之-tsar 2017-03-02 20:25 175人阅读 评论(0) 收藏 举报  分类: LINUX调优(9)    目 ...

  5. Real-time qPCR So Easy?

    Real-time qPCR So Easy? [2016-05-27]       实时荧光定量PCR技术是在定性RCR技术基础上发展起来的核酸定量技术,于1996年由美国Applied biosy ...

  6. BZOJ2330或洛谷3275 [SCOI2011]糖果

    BZOJ原题链接 洛谷原题链接 很明显的差分约束,但数据范围较大,朴素\(SPFA\)判正环求解会\(T\)(理论上如此,但我看到有挺多人用朴素的还跑得挺快..),所以需要优化. 我们所建立的有向图中 ...

  7. 1.3 Java中的标识符和关键字

    1.标识符 含义:标识符用于给程序中的类.变量.方法命名的符号. 组成:数字(0-9).字母(a-z)(A-Z).下划线(_).美元符号$. 命名规则:1.数字不能够作为命名符号的开头 2.不能够使用 ...

  8. Spring整合jedis 集群模式

    引入jedis依赖 <dependency> <groupId>redis.clients</groupId> <artifactId>jedis< ...

  9. 视频基础知识:浅谈视频会议中H.264编码标准的技术发展

    浅谈视频会议中H.264编码标准的技术发展 浅谈视频会议中H.264编码标准的技术发展 数字视频技术广泛应用于通信.计算机.广播电视等领域,带来了会议电视.可视电话及数字电视.媒体存储等一系列应用,促 ...

  10. 进入快速通道的委托(深入理解c#)

    1.方法组:所有的名称相同的重载方法合在一起就成为一个方法组. 2.协变性和逆变性: 协变性指的是——泛型类型参数可以从一个派生类隐式转化为基类. 逆变性指的是——泛型类型参数可以从一个基类隐式转化为 ...