Bone Collector II

Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3355    Accepted Submission(s):
1726

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
 
Author
teddy
 
Source
 
Recommend
teddy   |   We have carefully selected several similar
problems for you:  2159 1114 3033 1561 1712 
 
还是01背包问题,不过我是第一次写这种类型的题,参考了别人的代码和思想,差不多是一道模板第k大价值题,值得一做。
更新的时候有两个数组A、B,A记录选择第i个物品的从大到小排列,B记录不选择的从大到小排列,然后合并AB,选出AB里面前k个最大的。合并到dp中。。。
 
题意:第一行三个数,第一个数为物体数量,第二个数总体积,第三个数第k优解。注意!接下来第二行是物体的价值,第三行是物体的体积。
求第k大优解的值是多少。
 
附上代码:
 
 #include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int main()
{
int T,i,j,n,m,v,k,kk;
int dp[][],val[],vol[],A[],B[];
scanf("%d",&T);
while(T--)
{
scanf("%d%d%d",&n,&v,&k);
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]; //选中i物体
B[kk]=dp[j][kk]; //不选i物体
}
A[kk]=-; //-1标记为结尾
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 ;
}

hdu 2639 Bone Collector II(01背包 第K大价值)的更多相关文章

  1. HDU - 2639 Bone Collector II (01背包第k大解)

    分析 \(dp[i][j][k]\)为枚举到前i个物品,容量为j的第k大解.则每一次状态转移都要对所有解进行排序选取前第k大的解.用两个数组\(vz1[],vz2[]\)分别记录所有的选择情况,并选择 ...

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

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

  3. HDU 2639 Bone Collector II(01背包变型)

    此题就是在01背包问题的基础上求所能获得的第K大的价值. 详细做法是加一维去推当前背包容量第0到K个价值,而这些价值则是由dp[j-w[ i ] ][0到k]和dp[ j ][0到k]得到的,事实上就 ...

  4. HDU 2639 Bone Collector II (01背包,第k解)

    题意: 数据是常规的01背包,但是求的不是最大容量限制下的最佳解,而是第k佳解. 思路: 有两种解法: 1)网上普遍用的O(V*K*N). 2)先用常规01背包的方法求出背包容量限制下能装的最大价值m ...

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

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

  6. HDOJ(HDU).2602 Bone Collector (DP 01背包)

    HDOJ(HDU).2602 Bone Collector (DP 01背包) 题意分析 01背包的裸题 #include <iostream> #include <cstdio&g ...

  7. 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 ...

  8. hdu 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 (dp)

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

随机推荐

  1. 常用css3属性

    总结一下在工作用常用到的属性设置 1.设置文本的可选择性 -webkit-user-select:none/text 2.设置背景的绘制区域 background-clip:border-box/pa ...

  2. JSP页面中验证码的调用方法

    步骤: 1.首先是要生成验证码 2.对验证码类进行调用:主要 实现的是  将验证码图片 输出到response.getOutputStream()这个输出流中 调用时,可以在页面调用,也可以在serv ...

  3. 第二周<线性回归>

    可行性分析 略 sklearn.linear_model.linear_regression() 一些参数 fit_intercept 布尔型参数,表示是否计算该模型的截距 normalize 布尔型 ...

  4. WordPress使用自定义文章类型实现任意模板的方法和怎么做邮件回复

    主要就是使用了register_post_type 函数. 1.创建插件目录 新建一个文件夹用来存放插件文件,这里我就命名这个文件夹为myMood 2.创php代码文件 在刚才创建的文件夹里面新建一个 ...

  5. PHP小知识总结(1)

    1. mysqli_query — 对数据库执行一次查询 失败时返回 FALSE ,通过 mysqli_query() 成功执行SELECT, SHOW, DESCRIBE或 EXPLAIN查询会返回 ...

  6. linux C 编译时手动链接遇到的问题(未解决)

    写多线程的时候,编译的时候遇到了问题,开始的时候是这样的: 编译器不认识pthread_create和pthread_join这两个函数. 搜了一下原因是没有链接相应的库,下面是我看到一个博友写的: ...

  7. @codeforces - 1276F@ Asterisk Substrings

    目录 @description@ @solution@ @accepted code@ @details@ @description@ 给定一个包含 n 个小写字母的字符串 s,用 s 生成 n 个串 ...

  8. 2019-4-7-VisualStudio-解决方案筛选器-slnf-文件

    title author date CreateTime categories VisualStudio 解决方案筛选器 slnf 文件 lindexi 2019-04-07 11:34:59 +08 ...

  9. HZOJ 简单的期望

    性质:一个数分解质因数后2的次数=二进制下末尾连续0的个数. 乘2比较好考虑,比较恶心的是+1.一个$k*2^0$的数+1后可能会出现很多情况.但是k这个数表示不出来. 但是加的操作最多有200次,也 ...

  10. inflate用一个XML源填充view. LayoutInflater

    java.lang.Object     android.view.LayoutInflater This class is used to instantiate layout XML file i ...