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. 一条查询SQl是怎样执行的

    MySQL的逻辑架构图 大体来说,MySQL可以分为Server层和存储引擎层两部分. Server层包括连接器.查询缓存.分析器,优化器等,涵盖MySQL的大多核心服务功能,以及所有的内置函数,存储 ...

  2. 【Linux】centos7中 root家目录中perl5文件夹无法删除问题解决

    由于新项目上线,安装了一些perl的一些包 但是发现,在/root下有一个perl5/的文件夹,删除后,重新登录又会出现,很是烦人,而且他还没有内容,就是一个空文件 那么着手搞掉他 环境:centos ...

  3. SDUST数据结构 - 19级期末考试

    判断题: 选择题: 函数题: 6-1 统计二叉树叶子结点个数: 题目: 裁判测试程序样例: #include <stdio.h> #include <stdlib.h> typ ...

  4. oracle move表空间(分区表,索引)

    1.修改分区表分区表空间 SELECT 'ALTER TABLE ' || table_owner || '.' || TABLE_NAME || ' MOVE PARTITION ' || PART ...

  5. centos7下 开启/关闭/查看firewall运行状态命令

    1.开启防火墙:systemctl start firewalld.service [root@localhost bin]# systemctl start firewalld.service [r ...

  6. 1.2V升压5V和2.4V升压5V芯片,适用于镍氢电池产品

    一节镍氢电池1.2V升压5V电路芯片,两节镍氢电池2.4V升压到5V的电源芯片 PW5100具有宽范围的输入工作电压,从很低的0.7V到5V电压. PW5100输出电压固定3V,3.3V,5V可选固定 ...

  7. 计算机之路 -MySQL 初学

    照着电脑学了一天终于把MySQL装上了. 明天打算重新装一次 然后再自己记录一下步骤

  8. 前端知识(二)04-vue-element-admin-谷粒学院

    目录 一.vue-element-admin 1.简介 2.安装 二.vue-admin-template 1.简介 2.安装 一.vue-element-admin 1.简介 vue-element ...

  9. watchdog应用实例

    watchdog应用实例 By 鬼猫猫 20130504 http://www.cnblogs.com/muyr/ 实例:监测某文件夹,一旦文件夹里有文件,就把它剪切到其他服务器 import sys ...

  10. vue原生文件上传,可以多文件上传

    1.单文件上传 <template> <div> <label for="fileInput"> <i aria-hidden=" ...