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

这题很巧妙,用的是01背包思想,普通的01背包求的是最优解,但是题目要求的是第K最大值,所以我们要多加一维状态,即dp[j][k]表示质量为j的第k大值,那么用a[],b[]记录dp[j][h]和dp[j-w[i]][h]+v[i](h从1~k)。然后通过这两个数组求出k个依次排列的最大值dp[j][k].

#include<stdio.h>
#include<string.h>
int v[106],w[105],dp[1006][50];
int main()
{
int n,m,i,j,T,a[200],b[200],h,k,t,x,y;
scanf("%d",&T);
while(T--)
{
scanf("%d%d%d",&n,&m,&k);
for(i=1;i<=n;i++){
scanf("%d",&v[i]);
}
for(i=1;i<=n;i++){
scanf("%d",&w[i]);
}
memset(dp,0,sizeof(dp));
for(i=1;i<=n;i++){
for(j=m;j>=w[i];j--){
for(h=1;h<=k;h++){
a[h]=dp[j][h];
b[h]=dp[j-w[i]][h]+v[i];
}
a[k+1]=-1;b[k+1]=-1;
h=0;x=y=1;
while(h<k && (x<=k || y<=k)){
if(a[x]>b[y]){
t=a[x++];
}
else t=b[y++];
if(h==0 ||(h>0 && dp[j][h]!=t)){
h++;dp[j][h]=t;
}
}
}
}
printf("%d\n",dp[m][k]);
}
return 0;
}

hdu2639 Bone Collector II的更多相关文章

  1. hdu-2639 Bone Collector II 背包第K优

    http://acm.hdu.edu.cn/showproblem.php?pid=2639 在背包的基础上维护一个size<=K的最大值集合,为什么维护K个就好了呢,因为如果当前状态有多余K个 ...

  2. HDU--2639 Bone Collector II(01背包)

    题目http://acm.hdu.edu.cn/showproblem.php?pid=2639 分析:这是求第K大的01背包问题,很经典.dp[j][k]为背包里面装j容量时候的第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. Bone Collector II

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

  5. hdu 2639 Bone Collector II

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

  6. Bone Collector II(HDU 2639 DP)

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

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

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

  8. HUD 2639 Bone Collector II

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

  9. hdu 2639 Bone Collector II(01背包 第K大价值)

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

随机推荐

  1. 区间合并 C++

    #include <iostream> #include <vector> #include <algorithm> using namespace std; ty ...

  2. Openstack neutron 网络服务 (七)

    引用: https://docs.openstack.org/ocata/zh_CN/install-guide-rdo/common/get-started-networking.html neut ...

  3. 【Linux】dlopen failed: /lib/lsiRAID.so: cannot open shared object file: No such file or directory

    遇到这个问题,首先第一反应,是看其他的服务器中是否有这个库文件,如果有的话直接cp过来一份就行 但是检查发现,其他的系统中也不存在lsiRAID.so这个库文件,很神奇.. 但是看日志持续报错,查看s ...

  4. 【EXPDP/IMPDP】数据泵导入导出遇到目录没有权限问题

    当执行数据泵导出的时候,报了如下错误: ORA-39002: invalid operation ORA-39070: Unable to open the log file. ORA-39087: ...

  5. PYTHON爬虫实战_垃圾佬闲鱼爬虫转转爬虫数据整合自用二手急速响应捡垃圾平台_3(附源码持续更新)

    说明 文章首发于HURUWO的博客小站,本平台做同步备份发布. 如有浏览或访问异常图片加载失败或者相关疑问可前往原博客下评论浏览. 原文链接 PYTHON爬虫实战_垃圾佬闲鱼爬虫转转爬虫数据整合自用二 ...

  6. 2021年【线上】lammps分子动力学技术实战培训班

    材料模拟分子动力学课程 3月19号--22号 远程在线课 lammps分子动力学课程 3月12号--15号 远程在线课 第一性原理VASP实战课 3月25号-28号 远程在线课 量子化学Gaussia ...

  7. uni-app请求uni.request封装使用

    对uni.request的一些共同参数进行简单的封装,减少重复性数据请求代码.方便全局调用. 先在目录下创建 utils 和 common 这2个文件夹 utils 是存放工具类的,common 用来 ...

  8. unstable sort

    $sort (aggregation) - MongoDB Manual https://docs.mongodb.com/manual/reference/operator/aggregation/ ...

  9. Scheduling Multithreaded Computations by Work Stealing

    steal.pdf http://supertech.csail.mit.edu/papers/steal.pdf This paper studies the problem of eciently ...

  10. etcd 与 Zookeeper、Consul 等其它 kv 组件的对比

    基于etcd的分布式配置中心 etcd docs | etcd versus other key-value stores https://etcd.io/docs/v3.4.0/learning/w ...