Bone Collector II

Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 3042 Accepted Submission(s): 1578

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

背包求第K大的值,DP[i][j][k]表示放i件物品体积为V的时候第K大的值

for(int j=V;j>=v[i];j--)
{
for(int s=1;s<=k;s++)
{
A[top++]=Dp[j-v[i]][s]+w[i];
A[top++]=Dp[j][s];
}
}

表示将体积为V时,所有的情况,从中选出前K大的值,对于放每件物品所达到的体积都选出前K大的值,一直贪心到将所有的物品都放完,得到的DP[n][v][k]就是所求的.

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
#include <algorithm>
using namespace std;
typedef long long LL;
const int MAX = 1100;
int Dp[MAX][35];
int w[110],V[110];
int A[35];
int B[35];
int main()
{
int T;
int n,v,k;
scanf("%d",&T);
while(T--)
{
scanf("%d %d %d",&n,&v,&k);
for(int i=1;i<=n;i++)
{
scanf("%d",&w[i]);
}
for(int i=1;i<=n;i++)
{
scanf("%d",&V[i]);
}
memset(Dp,0,sizeof(Dp));
for(int i=1;i<=n;i++)//转化为01背包减少时间复杂度
{
for(int j=v;j>=V[i];j--)
{
int s;
for( s=1;s<=k;s++)
{
A[s]=Dp[j-V[i]][s]+w[i];
B[s]=Dp[j][s];
}
A[s]=-1;
B[s]=-1;
int a=1,b=1;
for(s=1;s<=k&&(A[a]!=-1||B[b]!=-1);)//采用归并的方式,也可以用优先队列
{
if(A[a]>B[b])
{
Dp[j][s]=A[a];
a++;
}
else
{
Dp[j][s]=B[b];
b++;
}
if(Dp[j][s]!=Dp[j][s-1])
{
s++;
}
}
}
}
printf("%d\n",Dp[v][k]);
}
return 0;
}

Bone Collector II的更多相关文章

  1. HDU 3639 Bone Collector II(01背包第K优解)

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

  2. hdu 2639 Bone Collector II

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

  3. Bone Collector II(HDU 2639 DP)

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

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

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

  5. HUD 2639 Bone Collector II

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

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

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

  7. hdu–2369 Bone Collector II(01背包变形题)

    题意:求解01背包价值的第K优解. 分析: 基本思想是将每个状态都表示成有序队列,将状态转移方程中的max/min转化成有序队列的合并. 首先看01背包求最优解的状态转移方程:\[dp\left[ j ...

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

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

  9. HDU 2639 Bone Collector II (dp)

    题目链接 Problem Description The title of this problem is familiar,isn't it?yeah,if you had took part in ...

随机推荐

  1. node.js学习(1)

    新建便笺 3 node.js学习(1) 1)安装 http://nodejs.org/download/下载. 2)编写一个案例 var http=require("http"); ...

  2. int 与 Integer--话说数组转集合

    话说是自从JDK5后,而这就可以自动进行类型转换了. 当然,区别还是有的,就是对象和“非对象”什么的.可是,今天进行一个测试,出了一个小问题,现将代码贴下: 代码0:先来一个正常点的,用String进 ...

  3. int和long long有符号整形 负数比正数多一个

    int的负数比正数多一个,则有一个负数在int范围内没有对应的正数 最大正整数用十六进制,很容易表示:0x7f ff ff ff int num = 0x7fffffff; num = -num; p ...

  4. each实现原理

    <script> function isEach(arr, callback) { for (var i in arr) { callback(i, arr[i]); } }; funct ...

  5. JSP和servlet有什么区别?

    JSP和servlet有什么区别? JSP是Servlet技术的扩展,本质上是Servlet的简易方式,更强调应用的外表表达. JSP编译后是"类servlet". Servlet ...

  6. 用Commons-FileUpload组件实现文件上传

    需要用到Tomcat还有commons-fileupload-1.3.1.jar包和commons-io-2.4.jar包. 如果需要传一个文件,form表单必须有enctype="mult ...

  7. YbRapidSolution.MVC项目首页分页没有起作用

    @model YbRapidSolution.Mvc.Models.CmsPagerDataModel <nav> <ul class="pager"> & ...

  8. 夺命雷公狗ThinkPHP项目之----企业网站28之网站前台左侧导航的实现

    我们基于刚才在model层的找顶级分类的代码在进行修改即可: <?php namespace Home\Controller; use Think\Controller; class Commo ...

  9. 夺命雷公狗ThinkPHP项目之----企业网站8之栏目的添加完善(无限极分类的完成)

    我们刚才只是完成了添加的一部分,但是我们的上级分类也不能永远都是只有一个死的嘛,所以我们需要对她进行修改: 我们先将add方法里面的数据查出来再说: 然后在模板页进行遍历: 展示效果如下所示: 虽然是 ...

  10. lower power的physical library

    在一个cell library中,比较重要的是cell height,cell height由tracks来决定,track表示一个metal线的pitch. 一个cell通常被做成一定数量的trac ...