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. mongodb的连接和开启安全验证

    首先是启动mongodb a.打开cmd,cd进入mongodb的安装目录下的bin目录下面,执行 mongod --dbpath D:\MongoDBdata(数据存放的目录) 或者将mongodb ...

  2. 随机删除数据库N条记录

    delete from  table where newsID in(select top 50 newsID from  table order by newid())

  3. JS练习题 显示登入者相关好友

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  4. ios blog

    转得一个朋友的博客,大家可以看哈(主要时国外的) 主要分开发教程.示例项目.UI设计.问题解决几块. 开发教程: 即便过了入门阶段,还是要经常看看一些不错的实例教程. .http://mobile.t ...

  5. git操作??

    一直在搞git,但是难度真的很大,我的英语超烂,而申请git账号时全部是英文的,我就拿着翻译有道词典,必应.进行翻译,一个一个单词的往上面打,一张网页能翻译一下午,最后还是不知道应该具体怎么去操作,所 ...

  6. 使用UIL(Universal-Image-Loader)异步加载图片

    概要: Android-Universal-Image-Loader是一个开源的UI组件程序,该项目的目的是实现可重复使用的异步图像加载.缓存和显示.所以,如果你的程序里需要这个功能的话,使用它,因为 ...

  7. sql 存储过程参数是表类型,数据库中如何调用

    DECLARE @NEW_STUDENT as [CancelLendersContent] INSERT @NEW_STUDENT VALUES (0,0,0,'12345678912','张三', ...

  8. 转:Order&Shipping Transactions Status Summary

    详细内容: http://blog.csdn.net/pan_tian/article/details/7696528 WSH_DELIVERY_DETAILS.Release_Status can ...

  9. Visual Studio 2012 怪异的自动重启

    学生在做项目的过程中遇到这种问题: -------------- 用 Visual Studio 2012  开发W中eb 项目时,最近总是莫名其妙的自动重启. 后来试了一下,发现是只要在页面中输入 ...

  10. zw版【转发·台湾nvp系列Delphi例程】HALCON FillUpShape2

    zw版[转发·台湾nvp系列Delphi例程]HALCON FillUpShape2 procedure TForm1.Button1Click(Sender: TObject);var op : H ...