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. NAS基础知识

    一.什么是NAS 1.NAS的定义 NAS(Network Attached Storage:网络附属存储)按字面简单说就是连接在网络上,具备资料存储功能的装置,因此也称为"网络存储器&qu ...

  2. 【Java】流程控制 - 顺序结构、 选择(分支)结构(单分支、双分支、多分支、嵌套)、循环结构(for、while、do...while)、跳转语句(break、continue)

    流程控制语句结构 文章目录 流程控制语句结构 一. 顺序结构 1. 输出语句 2. 输入语句 3.code 二.复合语句 三. 分支结构 1. 条件判断 1.单分支结构 2.双分支结构 3.多分支结构 ...

  3. 【Docker】在Linux系统中安装Docker虚拟机、启动停止重启查看Docker命令

    安装Docker 文章目录 安装Docker Docker虚拟机架构 在Linux系统中安装Docker虚拟机 管理Docker虚拟机 参考资料 Docker虚拟机架构 Docker创建的所有虚拟实例 ...

  4. kubernets之pv以及pvc

    一 持久卷以及持久卷声明的由来 由于不管是哪种卷,开发者都需要提前预知kubernets集群里面的存储类型,这样就在一定程度上违背了kubernets集群的设计理念,kubernets的设计理念是在由 ...

  5. 企业项目迁移go-zero全攻略(一)

    作者:Mikael 最近发现 golang 社区里出了一个新兴的微服务框架.看了一下官方提供的工具真的很好用,只需要定义好 .api 文件模版代码都可以一键生成,只需要关心业务:同时 core 中的工 ...

  6. java 利用异或^进行加密

    package com.zcj.eg001; import java.nio.charset.Charset; import org.junit.Test; public class Encrypti ...

  7. [Usaco2005 Dec]Scales 天平

    题目描述 约翰有一架用来称牛的体重的天平.与之配套的是N(1≤N≤1000)个已知质量的砝码(所有砝码质量的数值都在31位二进制内).每次称牛时,他都把某头奶牛安置在天平的某一边,然后往天平另一边加砝 ...

  8. 一文搞定全场景K3s离线安装

    作者简介 王海龙,Rancher中国社区技术经理,负责Rancher中国技术社区的维护和运营.拥有6年的云计算领域经验,经历了OpenStack到Kubernetes的技术变革,无论底层操作系统Lin ...

  9. logging philosophy 日志哲学

    Go kit - Frequently asked questions https://gokit.io/faq/ Logging - Why is package log so different? ...

  10. Django runserver 默认多线程 监听文件变动

    django-admin and manage.py | Django documentation | Django https://docs.djangoproject.com/en/3.0/ref ...