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

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
题目大意:
输入n,v,k分别代表n个物品,v的体积,以及要求v能装下第k大的价值。
01背包变形,加一维代表第几大,最后dp[v][k]即为答案。
#include <iostream>
#include <cstring>
using namespace std;
int v[],w[];
int dp[][],a[],b[];
int n,val,k;
int main()
{
int T;
cin>>T;
while(T--)
{
memset(dp,,sizeof dp);
cin>>n>>val>>k;
for(int i=;i<=n;i++)
cin>>v[i];
for(int i=;i<=n;i++)
cin>>w[i];
for(int i=;i<=n;i++)
for(int j=val;j>=w[i];j--)
{
for(int l=;l<=k;l++)
{
a[l]=dp[j][l];///不取
b[l]=dp[j-w[i]][l]+v[i];//取
}
a[k+]=b[k+]=-;
int x=,y=,z=;
while(z<=k&&(x<=k||y<=k))///更新,也可以直接排序
{
if(a[x]>b[y])
dp[j][z]=a[x++];
else
dp[j][z]=b[y++];
if(dp[j][z]!=dp[j][z-])
z++;
}
}
cout<<dp[val][k]<<'\n';
}
return ;
}

 

Bone Collector II(01背包kth)的更多相关文章

  1. HDU 2639 Bone Collector II(01背包变形【第K大最优解】)

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

  2. hdu–2369 Bone Collector II(01背包变形题)

    题意:求解01背包价值的第K优解. 分析: 基本思想是将每个状态都表示成有序队列,将状态转移方程中的max/min转化成有序队列的合并. 首先看01背包求最优解的状态转移方程:\[dp\left[ j ...

  3. HDU 2639 Bone Collector II (01背包,第k解)

    题意: 数据是常规的01背包,但是求的不是最大容量限制下的最佳解,而是第k佳解. 思路: 有两种解法: 1)网上普遍用的O(V*K*N). 2)先用常规01背包的方法求出背包容量限制下能装的最大价值m ...

  4. HDU 2639 Bone Collector II(01背包变型)

    此题就是在01背包问题的基础上求所能获得的第K大的价值. 详细做法是加一维去推当前背包容量第0到K个价值,而这些价值则是由dp[j-w[ i ] ][0到k]和dp[ j ][0到k]得到的,事实上就 ...

  5. HDU - 2639 Bone Collector II (01背包第k大解)

    分析 \(dp[i][j][k]\)为枚举到前i个物品,容量为j的第k大解.则每一次状态转移都要对所有解进行排序选取前第k大的解.用两个数组\(vz1[],vz2[]\)分别记录所有的选择情况,并选择 ...

  6. HDOJ(HDU).2602 Bone Collector (DP 01背包)

    HDOJ(HDU).2602 Bone Collector (DP 01背包) 题意分析 01背包的裸题 #include <iostream> #include <cstdio&g ...

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

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

  8. hdu 2602 Bone Collector(01背包)模板

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2602 Bone Collector Time Limit: 2000/1000 MS (Java/Ot ...

  9. HDU2602 Bone Collector 【01背包】

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

  10. hdu2602 Bone Collector(01背包) 2016-05-24 15:37 57人阅读 评论(0) 收藏

    Bone Collector Problem Description Many years ago , in Teddy's hometown there was a man who was call ...

随机推荐

  1. 复习Java和前端、后端框架等。

    以下便是我开始复习时做的笔记.

  2. 使用PlSQLDeveloper工具查询Oracle会话数

    PlSQLDeveloper工具提供了会话管理功能. 能够查询会话内容.杀死会话.查看会话SQL等操作. 常用的会话查询SQL如下: -- 查询所有会话 select * from v$session ...

  3. 类成员的指针必须NULL化,否则是乱七八糟的东西

    class BiTree { public: BiTree(); virtual ~BiTree(); virtual void insertNode(Node * newNode); virtual ...

  4. 第3章 接口与API设计 52条笔记

    第3章 接口与API设计 52条笔记 第15条: 用前缀避免命名空间冲突 Objective-C 没有其他语言那种内置的命名空间机制 .鉴于此,我们在起名时要设法避免潜在的命名冲突,否则很容易就重名了 ...

  5. grep的几个参数

    -a 在二进制问就爱你中,以文本方式进行搜索 -c 计算找到搜索字符串的次数 -i 忽略大小写 -n 输出行号 -v 反向选择,即没有显示搜索字符串内容的那一行 grep -n '\.$'  file ...

  6. 工作中Git使用笔记

    git相关说明. //git 安装$ git config --global user.name "xxx"代码提交时的用户名,与GITLAB注册用户名建议保持一致$ git co ...

  7. sql 删除重复数据

    DELETE a FROM tbBuilding a WHERE EXISTS (SELECT 1 FROM tbBuilding b WHERE b.Province = a.Province AN ...

  8. Django展示第一个网页

    展示一个网页需要三部分组成: urls.py -- 指定网址与对应的视图 views.py -- 创建试图以及指定对应的模板 template/*.html -- 对应的模板 一.urls.py ur ...

  9. Mybatis(一)入门

    mybatis使用的三个部分数据查询主体 : SqlSession查询映射层 : Mapper接口数据维护层 : Bean 设计一.添加maven依赖<!-- mybatis依赖 -->& ...

  10. HashMap Hashtable TreeMap LinkedHashMap 分析

    首先对hash的了解:就是关键字,和数据建立关系的映射. hash常用算法:假设我们中的字符有相应的内部编码,当然在实际过程中,我们不可能将所有的编码当做hash值. 平方取中法,将所得的内部编码平方 ...