Bone Collector II

Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4824    Accepted Submission(s): 2514

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
 
Author
teddy
 
Source
 题意:
有n件物品,每件物品有价值和体积,有容量为m的背包,求能够得到的第k大的价值
代码:
  1. //以前的dp[V]数组再加一维dp[V]K]表示V状态时第k大的值,当枚举到第i个物品时
  2. //dp[i][V]=max(dp[i-1][V],dp[i-1][V-v]),当前状态由两个状态转移来的所以前k大的值
  3. //也是由两个状态的前k大的值转移来的。注意本体价值重复的算一个。
  4. #include<iostream>
  5. #include<cstdio>
  6. #include<cstring>
  7. using namespace std;
  8. const int MAXN=;
  9. const int MAXV=;
  10. const int MAXK=;
  11. int dp[MAXV][MAXK];
  12. int val[MAXN],vol[MAXN];
  13. int N,V,K;
  14. int main()
  15. {
  16. int t;
  17. scanf("%d",&t);
  18. while(t--){
  19. scanf("%d%d%d",&N,&V,&K);
  20. for(int i=;i<=N;i++)
  21. scanf("%d",&val[i]);
  22. for(int i=;i<=N;i++)
  23. scanf("%d",&vol[i]);
  24. memset(dp,,sizeof(dp));
  25. for(int i=;i<=N;i++){
  26. for(int j=V;j>=vol[i];j--){
  27. int a1=,a2=,p1[],p2[];
  28. for(int c=;c<=K;c++){
  29. p1[c]=dp[j][c];
  30. p2[c]=dp[j-vol[i]][c]+val[i];
  31. }
  32. p1[K+]=p2[K+]=-;
  33. int c=;
  34. while(c!=K){
  35. int tmp=max(p1[a1],p2[a2]);
  36. if(tmp==p1[a1]){
  37. a1++;
  38. if(tmp!=dp[j][c]) dp[j][++c]=tmp;
  39. else if(tmp==) dp[j][++c]=;
  40. }
  41. else if(tmp==p2[a2]){
  42. a2++;
  43. if(tmp!=dp[j][c]) dp[j][++c]=tmp;
  44. else if(tmp==) dp[j][++c]=;
  45. }
  46. }
  47. //cout<<i<<" "<<j<<endl;
  48. //for(int k=1;k<=K;k++) cout<<dp[j][k]<<" ";
  49. //cout<<endl;
  50. }
  51. }
  52. printf("%d\n",dp[V][K]);
  53. }
  54. return ;
  55. }
 

HDU 2639 背包第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. 01背包-第k优解

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

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

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

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

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

  9. Bone Collector II---hdu2639(01背包求第k优解)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2639求01背包的第k大解.合并两个有序序列 选取物品i,或不选.最终的结果,是我们能在O(1)的时间内 ...

随机推荐

  1. 一个简单的页面弹窗插件 jquery.pageMsgFrame.js

    页面弹窗是网站中常用的交互效果,它可以强提示网站的某些信息给用户,或者作用于某些信息的修改等等功能. 这几天在做一个项目的时候,就顺捎把这个插件写一下,栽棵树,自己乘凉吧. 原创博文,转载请注明出处: ...

  2. MR execution in YARN

    Overview YARN provides API not for application developers but for the great developers working on ne ...

  3. 第三周的psp

    PSP: 进度条: 累计进度图: 本周PSP饼状图:

  4. JavaScript初探系列之基本概念

    JavaScript的核心语言特性在ECMA-262中是以名为ECMAScript(ECMA, EuropeanComputer Manufacturers Association )的伪语言的形式来 ...

  5. 计算器软件实现系列(五)策略模式+asp.net

    一 策略模式代码的编写 using System; using System.Collections.Generic; using System.Linq; using System.Web; /// ...

  6. iOS开发解决页面滑动返回跟scrollView左右划冲突

    -(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithG ...

  7. thrift多平台安装

    thrift支持多语言的RPC,一直都想深入学习了解thrift,最近有空,就上网查了些资料,学习了一下,对它的使用有了一些了解.本篇是写thrift的安装,使用方法会另起一篇来写. 本文使用thri ...

  8. C# WebBrowser控件详解

     作者:827969653     0.常用方法 Navigate(string urlString):浏览urlString表示的网址 Navigate(System.Uri url):浏览url表 ...

  9. linux后台运行之screen和nohup

    3.1 nohup命令 如果你正在运行一个进程,而且你觉得在退出帐户时该进程还不会结束,那么可以使用nohup命令. 该命令可以在你退出帐户/关闭终端之后继续运行相应的进程. nohup就是不挂起的意 ...

  10. linq的decimal类型保存到数据库只保存到小数点后两位的问题

    今天的一个decimal类型保存到数据的问题困扰了我很长时间,最后就是一个小小的设置问题解决······坑······深坑···· 话不多说,直接说问题,在说答案: 问题:linq当采用EF的DbCo ...