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. nginx:403 forbidden 二种原因

    出现403 forbidden的两种原因:1.是缺少索引文件(index.html/inde.php):2.是权限问题 一.缺少索引文件index.html/inde.php 比如下面的配置: ser ...

  2. Lintcode: Segment Tree Query

    For an integer array (index from 0 to n-1, where n is the size of this array), in the corresponding ...

  3. Eclipse插件CheckStyle的安装和使用

    转载自:http://www.cnblogs.com/lanxuezaipiao/p/3202169.html CheckStyle是SourceForge下的一个项目,提供了一个帮助JAVA开发人员 ...

  4. SQL top order between 一起使用

    select * from emp;

  5. 在数组中搜索数据用 filteredArrayUsingPredicate

    苹果官方说明文档:https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/Predicates/Articles/ ...

  6. [转] MongoDB shell 操作 (查询)

    最近有用到mongoDB,每次都去查看官方文档很是费劲,自己准备写点东西.但在博客园上看到另外的一篇博文不错,就转载过来,加上点儿自己的修饰 左边是mongodb查询语句,右边是sql语句.对照着用, ...

  7. ActionController::InvalidAuthenticityToken 解决办法(第二种尤其有效)

    第一种:  Ror代码 class FooController < ApplicationController       protect_from_forgery :except => ...

  8. .net 中 ref out params的区别

    C#中有三个关键字-ref,out ,params,虽然本人不喜欢这三个关键字,因为它们疑似破坏面向对象特性.但是既然m$把融入在c#体系中,那么我们就来认识一下参数修饰符ref,out ,param ...

  9. bzoj1834 [ZJOI2010]network 网络扩容

    第一问跑最大流,第二问新建一条边连接0和1,流量为上第一问的答案+k,费用为0,接下来图中每条边拆成两条边,第一条容量为C费用为0,第二条容量无穷费用为W,再跑一遍费用流即可. 代码 #include ...

  10. android_demo01

    /layout/activity_main.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/ ...