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. zzulioj--1707--丧心病狂的计数(水题)

    1707: 丧心病狂的计数 Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 237  Solved: 105 SubmitStatusWeb Board ...

  2. Another app is currently holding the yum lock; waiting for it to exit…

    yum被锁定无法使用,错误信息截图如下:解决方法:rm -rf /var/run/yum.pid 来强行解除锁定,然后你的yum就可以运行了

  3. 升级Ubuntu18.04后遇到的坑

    升级过程:   直接do-release-update 就可以直接从16.04更新到18.04了. 中间会提升更新一些配置文件, 我大部分都选择了N. 然后就成功升级到18.04了, 显卡驱动什么的都 ...

  4. http协议无状态中的 "状态" 到底指的是什么?!(转载)

    转载自:https://www.cnblogs.com/bellkosmos/p/5237146.html   引子: 最近在好好了解http,发现对介绍http的第一句话[http协议是无状态的,无 ...

  5. Hbase项目(完整版)

    涉及概念梳理:命名空间 4.1.1.命名空间的结构 1) Table:表,所有的表都是命名空间的成员,即表必属于某个命名空间,如果没有指定,则在default默认的命名空间中. 2) RegionSe ...

  6. mybatis与spring整合配置

    mybatis与spring整合配置: 第一种方式:(此处配置扫描的包路径.注解.每个mapper类上面需要加@Repository才能纳入spring的bean管理器中) <!-- 自动扫描m ...

  7. HTML基础——网站首页显示页面

    1.表格标签: border设置边框,align设置位置(居中等),bgcolor设置背景颜色,cellspacing设置边框之间的空隙,cellpadding设置边框与里面内容的间距. table表 ...

  8. DotNetCore.1.0.1-VS2015Tools.Preview2.0.2 安装错误分析及解决办法(so far)

    折腾了这么多天总算弄完了,真恶心.为了让其他童靴避免掉进我遇到的坑里,我决定把最近遇到问题及其解决办法总结一下,希望对大家有帮助. 1.对于2016年7月底以前安装VS用户来说,可能不会那么迫切安装这 ...

  9. 使用3ds Max制作卡通狗教程

    使用软件::3ds Max 软件下载:http://www.xy3dsmax.com/xiazai.html 全教程完,学完记得交作业.如果本教程对您有所帮助,请推荐给你的朋友. 全教程完,学完记得交 ...

  10. vector ----- size函数注意事项

    vector 的size函数返回vector大小,返回值类型为size_type,Member type size_type is an unsigned integral type,即无符号整数: ...