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
#include <iostream>
#include <cstdio>
using namespace std;
#define max(a,b) ((a)>(b)?(a):(b))
const int maxn = ;
int main()
{
int T;
scanf("%d", &T);
int dp[maxn][], val[maxn], vol[maxn], A[], B[];
while (T--)
{
int n, v, k;
scanf("%d %d %d", &n, &v, &k);
int i, j, kk;
for (i=; i<n; i++) scanf("%d", &val[i]);
for (i=; i<n; i++) scanf("%d", &vol[i]);
memset(dp, , sizeof(dp)); int a, b, c;
for (i=; i<n; i++)
for (j=v; j>=vol[i]; j--)
{
for (kk=; kk<=k; kk++)
{
A[kk] = dp[j-vol[i]][kk] + val[i];
B[kk] = dp[j][kk];
}
A[kk] = -, B[kk] = -;
a = b = c = ;
while (c<=k && (A[a] != - || B[b] != -))
{
if (A[a] > B[b])
dp[j][c] = A[a++];
else
dp[j][c] = B[b++];
if (dp[j][c] != dp[j][c-])
c++;
}
} printf("%d\n", dp[v][k]);
}
return ;
}

01背包-第k优解的更多相关文章

  1. HDU 2639 (01背包第k优解)

    /* 01背包第k优解问题 f[i][j][k] 前i个物品体积为j的第k优解 对于每次的ij状态 记下之前的两种状态 i-1 j-w[i] (选i) i-1 j (不选i) 分别k个 然后归并排序并 ...

  2. (01背包 第k优解) Bone Collector II(hdu 2639)

    http://acm.hdu.edu.cn/showproblem.php?pid=2639       Problem Description The title of this problem i ...

  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. 杭电 2639 Bone Collector II【01背包第k优解】

    解题思路:对于01背包的状态转移方程式f[v]=max(f[v],f[v-c[i]+w[i]]);其实01背包记录了每一个装法的背包值,但是在01背包中我们通常求的是最优解, 即为取的是f[v],f[ ...

  5. hdu2639 01背包第K优解

    #include<iostream> #include<cstdio> #include<algorithm> #include<cstring> #i ...

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

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

  7. HDU 2639 背包第k优解

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

  8. HDU 2639 Bone Collector II【01背包 + 第K大价值】

    The title of this problem is familiar,isn't it?yeah,if you had took part in the "Rookie Cup&quo ...

  9. hdu 2639 Bone Collector II (01背包,求第k优解)

    这题和典型的01背包求最优解不同,是要求第k优解,所以,最直观的想法就是在01背包的基础上再增加一维表示第k大时的价值.具体思路见下面的参考链接,说的很详细 参考连接:http://laiba2004 ...

随机推荐

  1. bzoj1797: [Ahoi2009]Mincut 最小割(最小割+强联通tarjan)

    1797: [Ahoi2009]Mincut 最小割 题目:传送门 题解: 感觉是一道肥肠好的题目. 第二问其实比第一问简单? 用残余网络跑强联通,流量大于0才访问. 那么如果两个点所属的联通分量分别 ...

  2. CoreData 从入门到精通(二) 数据的增删改查

    在上篇博客中,讲了数据模型和 CoreData 栈的创建,那下一步就是对数据的操作了.和数据库一样,CoreData 里的操作也无非是增删改查.下面我们将逐步讲解在 CoreData 中进行增删改查的 ...

  3. centos6.0 配置SVN

    基本步骤: 1.安装必需的subversion 2.创建版本库 3.配置用户和权限 4.钩子和svn常用命令说明 一.安装subversion 在这里我们使用yum来安装subversion,使用以下 ...

  4. 关于markdown的使用

    首先: https://www.cnblogs.com/jordangong/p/9804777.html 注意:提交博客时需将 Markdown 源码粘贴到编辑器中,且编辑器没有实时预览,可以上传后 ...

  5. 26.boost文件库

    #define _CRT_SECURE_NO_WARNINGS #include <boost/filesystem/operations.hpp> #include <boost/ ...

  6. (转载)自定义View——弹性滑动

    滑动是Android开发中非常重要的UI效果,几乎所有应用都包含了滑动效果,而本文将对滑动的使用以及原理进行介绍. 一.scrollTo与ScrollBy View提供了专门的方法用于实现滑动效果,分 ...

  7. php--tp5在查询到的数据中添加新字段

  8. 《Unix环境高级编程》读书笔记 第10章-信号

    1.引言 信号是软件中断. 信号提供了一种处理异步事件的方法. 2. 信号概念 信号的名字都是以3个字符SIG开头. Linux3.2.0支持31种信号.FreeBSD.Linux和Solaris作为 ...

  9. 线段树(segment tree )

    http://www.cnblogs.com/TenosDoIt/p/3453089.html 写的非常好! 很多学校流行一种比较的习惯.老师们很喜欢询问,从某某到某某当中,分数最高的是多少. 这让很 ...

  10. dede内链怎么优化,Dedecms内部链接优化技巧

    dede内链怎么优化,dedecms内部链接优化技巧 使用dedecms的过程中发现,可以通过dedecms的文档关键词维护功能.发表文章时候的关键词添加功能(也可以自动获取)以及核心设置里面的是否使 ...