Bone Collector II

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

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
 
 
题目的意思就是求01背包的第k优解,则自然想到(我感觉一点都不自然)多一维,dp【j】【k】;
状态dp【j】的前k个最优解,都是由dp[j][1....k]和dp[j-w[i]][1.....k]+v[i]转移过来(没有证明过,但是对的),可以用优先队列来维护。
在求解dp[j][k]时,我们首先把dp[j][1....k]和dp[j-w[i]][1.....k]+v[i]统统放进优先队列(会自己从大到小排),然后我们依次拿出k个,放进dp[j][1.....k]就ok了,但是要避免重复。
#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
int main()
{
int T;
int dp[][];
cin >> T;
priority_queue<int>q;//默认从大到小排
while (T--)
{
memset(dp, , sizeof(dp));
int n, vv, kk;
cin >> n >> vv >> kk;
int i, j, k;
int v[], w[];
for (i = ; i <= n; i++)
cin >> v[i];
for (i = ; i <= n; i++)
cin >> w[i];
for (i = ; i <= n; i++)
{
for (j = vv; j >= w[i]; j--)//01背包的循环
{
while (!q.empty()) q.pop();
for (k = ; k <= kk; k++)
{//dp[j][1....k]和dp[j-w[i]][1.....k]+v[i]放进队列
q.push(dp[j][k]);
q.push(dp[j - w[i]][k] + v[i]);
}
k = ;
while ()
{
if (q.empty() || k == kk+) break;
if (k > && q.top() != dp[j][k-])
{//这一步避免重复, q.top() == dp[j][k-1]要排除
dp[j][k] = q.top(); k++;
}
else if (k == )
{
dp[j][k] = q.top(); k++;
}
q.pop();
}
}
}
cout << dp[vv][kk] << endl;
}
return ; }
 
 

HUD 2639 Bone Collector II的更多相关文章

  1. HDU 2639 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. hdu 2639 Bone Collector II(01背包 第K大价值)

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

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

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

  6. 杭电 2639 Bone Collector II【01背包第k优解】

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

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

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

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

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

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

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

随机推荐

  1. 《Python》 字典

    一.字典 字典是Python的基础数据类型之一: 字典可以存储大量的数据,关系型数据: 同样他也是Python中唯一的映射类数据类型. 数据类型的分类: 可变的(不可哈希的)数据类型:list,dic ...

  2. tp5中捕获异常的配置

    首选在配置文件中加入配置如下 // 异常处理handle类 留空使用 \think\exception\Handle    'exception_handle'       => '\\app\ ...

  3. linux 优化git操作速度

    修改 ssh配置:useDNS:no

  4. putc,fputc,和putchar

    putc()功能和fputc()差不多,一个是宏,一个是函数 putc(int ch,FILE *fp),即将字符ch输出到fp所指的文件中:putchar(char ch),即将字符ch输出到标准输 ...

  5. 如何从github上clone项目源码-linux

    前言 github是目前较为流行的代码托管网站,linux系统是目前开发人员较为常用的操作系统.项目实现的过程中用到一些经典好用的源代码,可以从github上clone,本文主要介绍linux系统命令 ...

  6. android复制包需要修改的几个地方

    1.要看什么情况,若是在同一个eclipse下,那么就需要修改包名.若不在的话,那就可以不用了. 2.这个app_name也是一样. 3.先说下情况,这是我修改好的.原本这个R的是引之前包的,必须要改 ...

  7. 【转载】Python字符串操作之字符串分割与组合

    1. str.split():字符串分割函数 通过指定分隔符对字符串进行切片,并返回分割后的字符串列表. 语法: str.split(s, num)[n] 参数说明: s:表示指定的分隔符,不写的话, ...

  8. Vue2.x directive自定义指令

    directive自定义指令 除了默认设置的核心指令( v-model 和 v-show ),Vue 也允许注册自定义指令. 注意,在 Vue2.0 里面,代码复用的主要形式和抽象是组件——然而,有的 ...

  9. scylladb docker 运行试用

      scylladb 是兼容cassandra 的数据存储系统,从官方的性能报告,比原生的apache cassandra 有好多 的提高 使用docker 运行,具体的也可以参考官方文档,后边会提供 ...

  10. log parser 微软iis 日志分析

    Log Parser 2.2 您可以从 Microsoft 下载中心下载 Log Parser. Log Parser 2.2 是一个功能强大的通用工具,它可对基于文本的数据(如日志文件.XML 文件 ...