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 2 31). 
Sample Input

  1. 3
  2. 5 10 2
  3. 1 2 3 4 5
  4. 5 4 3 2 1
  5. 5 10 12
  6. 1 2 3 4 5
  7. 5 4 3 2 1
  8. 5 10 16
  9. 1 2 3 4 5
  10. 5 4 3 2 1

Sample Output

  1. 12
  2. 2
  3. 0
  1. #include <iostream>
  2. #include <cstdio>
  3. using namespace std;
  4. #define max(a,b) ((a)>(b)?(a):(b))
  5. const int maxn = ;
  6. int main()
  7. {
  8. int T;
  9. scanf("%d", &T);
  10. int dp[maxn][], val[maxn], vol[maxn], A[], B[];
  11. while (T--)
  12. {
  13. int n, v, k;
  14. scanf("%d %d %d", &n, &v, &k);
  15. int i, j, kk;
  16. for (i=; i<n; i++) scanf("%d", &val[i]);
  17. for (i=; i<n; i++) scanf("%d", &vol[i]);
  18. memset(dp, , sizeof(dp));
  19.  
  20. int a, b, c;
  21. for (i=; i<n; i++)
  22. for (j=v; j>=vol[i]; j--)
  23. {
  24. for (kk=; kk<=k; kk++)
  25. {
  26. A[kk] = dp[j-vol[i]][kk] + val[i];
  27. B[kk] = dp[j][kk];
  28. }
  29. A[kk] = -, B[kk] = -;
  30. a = b = c = ;
  31. while (c<=k && (A[a] != - || B[b] != -))
  32. {
  33. if (A[a] > B[b])
  34. dp[j][c] = A[a++];
  35. else
  36. dp[j][c] = B[b++];
  37. if (dp[j][c] != dp[j][c-])
  38. c++;
  39. }
  40. }
  41.  
  42. printf("%d\n", dp[v][k]);
  43. }
  44. return ;
  45. }

01背包-第k优解的更多相关文章

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

  2. (01背包 第k优解) Bone Collector II(hdu 2639)

    http://acm.hdu.edu.cn/showproblem.php?pid=2639       Problem Description The title of this problem i ...

  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. hdu2639 01背包第K优解

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

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

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

  7. HDU 2639 背包第k优解

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

  8. HDU 2639 Bone Collector II【01背包 + 第K大价值】

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

  9. hdu 2639 Bone Collector II (01背包,求第k优解)

    这题和典型的01背包求最优解不同,是要求第k优解,所以,最直观的想法就是在01背包的基础上再增加一维表示第k大时的价值.具体思路见下面的参考链接,说的很详细 参考连接:http://laiba2004 ...

随机推荐

  1. String转换成int型

    private boolean judge(String str){ int year = 0; try{ year = Integer.valueOf(str).intValue(); }catch ...

  2. 跨域调用接口——WebClient通过get和post请求api

    AJAX不可以实现跨域请求,经过特殊处理才行.一般后台可以通过WebClient实现跨域请求~ //get 请求        string url = string.Format("htt ...

  3. JS循环 for while 全局/局部变量 短路

    循环语句: For for循环的格式 for(var i = 0; i < 10; i ++){ } for循环的执行顺序: ①   ② 若判断为 true  进④  进③ 进②判断 ……循环 ...

  4. vmware workstation中的NAT配置

    宿主机:win10: IP:192.168.1.101 GW:192.168.1.1 以太网2(VMNET8) IP:192.168.100.1 GW:nonevmware中的虚拟网络设置(NAT): ...

  5. Session与Token认证机制 前后端分离下如何登录

     字号 1 Web登录涉及到知识点 1.1 HTTP无状态性 HTTP是无状态的,一次请求结束,连接断开,下次服务器再收到请求,它就不知道这个请求是哪个用户发过来的.当然它知道是哪个客户端地址发过来的 ...

  6. 微信小程序手势滑动卡片案例

    最近工作中有项目要使用微信小程序技术进行开发,其中一项功能困扰了我很久,卡片滑动动效以及手势识别.经过一番研究和参考,现在把成果展示.记录自己踩到的坑,如果大家有需要,也可以帮助到大家. 效果图: 首 ...

  7. centos7-centos6常用配置对比

    设置(CentOS 6 vs CentOS 7)系统常用配置 ysvinit vs Upstart vs Systemd) 常见设置: 字符集CentOS 6方法:/etc/sysconfig/i1 ...

  8. pandas 4 处理缺失数据nan

    from __future__ import print_function import pandas as pd import numpy as np np.random.seed(1) dates ...

  9. 怎么给Unity写一个原生的插件

    本文章由cartzhang编写,转载请注明出处. 所有权利保留. 文章链接:http://blog.csdn.net/cartzhang/article/details/50266889 作者:car ...

  10. C语言修改文件某部分内容

    两种方法 1.全部读入内存 修改后重新存入文件 2.边读边写到另一新建文件 要修改的部分修改后存入新建文件 其他部分原封不动写入 写完删掉原先文件 将这个新的改为删掉那个的名字 方法一 读入内存修改 ...