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. mmall商城分类模块总结

    后台分类model的开发具体功能有:添加分类名称,修改分类名称,查询所有子分类,查询父分类以及它下面的子分类(递归) 需要注意的是,在后台管理进行操作的时候,都需要验证当前用户是否是管理员的角色,不管 ...

  2. .NET探索平台条件编译

    前言 今天偶然机会,翻了一下大学期间的书籍<C程序设计>,好吧,当我翻着翻着,翻到了符号常量(#define指令)中,是啊,这是一个预处理器指令,记得在Magicodes.IE中针对平台选 ...

  3. kubernets集群的安全防护(上)

    一  了解认证机制 1.1   API的服务器在接收来自客户端的请求的时候会对发起的用户进行几个步骤 认证插件进行认证,确认发起的用户是外部用户,还是集群中的某个命名空间里面的pod 确认用户属于哪个 ...

  4. 攻防世界-crypto-easychallenge(.pyc反编译)

    进入题目后下载附件,发现是一个.pyc文件. pyc是一种二进制文件,是由py文件经过编译后,生成的文件,是一种byte code,py文件变成pyc文件后,运行加载的速度会有所提高:另一反面,把py ...

  5. 5.2 Spring5源码--Spring AOP源码分析二

    目标: 1. 什么是AOP, 什么是AspectJ 2. 什么是Spring AOP 3. Spring AOP注解版实现原理 4. Spring AOP切面原理解析 一. 认识AOP及其使用 详见博 ...

  6. 记录Js动态加载页面.append、html、appendChild、repend添加元素节点不生效以及解决办法

    今天再优化blog页面的时候添加了个关注按钮和图片,但是页面上这个按钮和图片时有时无,本来是搞后端的,被这个前端的小问题搞得抓耳挠腮的! 网上各种查询解决方案,把我解决问题的艰辛历程分享出来,希望大家 ...

  7. 指针锁定 Pointer Lock API 用法

    指针锁定 Pointer Lock API 通过它可以访问原始的鼠标运动(基于指针的相对位移 movementX / movementY),把鼠标事件的目标锁定到一个特定的元素,同时隐藏视图中的指针光 ...

  8. three.js cannon.js物理引擎之Heightfield

    今天郭先生说一说cannon.js物理引擎之Heightfield高度场,学过场论的朋友都知道物理学中把某个物理量在空间的一个区域内的分布称为场,高度场就是与高度相关的场,而cannon.js物理引擎 ...

  9. 冷饭新炒:理解JDK中UUID的底层实现

    前提 UUID是Universally Unique IDentifier的缩写,翻译为通用唯一标识符或者全局唯一标识符.对于UUID的描述,下面摘录一下规范文件A Universally Uniqu ...

  10. CODING 再携手腾讯云 Serverless,让开发者跑步上云

    近年来,腾讯云持续在云原生领域打磨和完善产品矩阵,致力于为开发者上云提供更好的产品和服务.继前段时间 CODING CI 助力腾讯云 Serverless 全新应用控制台.持续保障 Serverles ...