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. C++面向对象多线程入门

    第1节   背景 为了更好的理解多线程的概念,先对进程,线程的概念背景做一下简单介绍. 早期的计算机系统都只允许一个程序独占系统资源,一次只能执行一个程序.在大型机年代,计算能力是一种宝贵资源.对于资 ...

  2. Elasticsearch利用scroll查询获取所有数据

    Elasticsearch有两种分页方式,一种是通过from和size条件来实现,但是该方法开销比较大,另一种是利用scroll来实现,通过scroll来实现分页获取所有的数据,下面是利用python ...

  3. pdo::quey excu excute 区别

    PDO::query(PHP环境下同)和PDOStatement::execute函数均能实现SELECT查询功能,但官方文档并未见对此设计初衷的说明,此外还有个PDO::exec函数功能也很类似.天 ...

  4. cocos2dx 不同平台上加载文件

    原文转自:http://blog.sina.com.cn/s/blog_62b2318d0101eozt.html cocos2dx在不同平台上读取资源文件时的处理方式是不同的. 在ios下,程序调用 ...

  5. isKindOfClass in cocos2d-x

    在最新版2.*的cocos2d-x中isKindOfClass可以用如下代码代替. 未验证,不过看了引擎代码是这样写的   原代码 [s1 isKindOfClass:[DestHole class] ...

  6. Java平台标准版本

    JDK   Java Language Java Language     Tools &Tool APIs java javac javadoc jar javap jdeps Script ...

  7. 那些年,追寻JMeter的足迹,免费送……

    ​我们测试技术部武汉团队自16年引入jmeter以来,利用jmeter做了很多事情.首先运用jmeter进行接口测试,后续实现jmeter自动化冒烟测试,顺带也实现了线上环境的巡检,节省了大量人力.j ...

  8. OC基础:内存(进阶):retain.copy.assign的实现原理 分类: ios学习 OC 2015-06-26 17:36 58人阅读 评论(0) 收藏

    遍历构造器的内存管理 a.遍历构造器方法内部使用autorelease释放对象 b.通过遍历构造器生成的对象.不用释放. 内存的管理总结 1.想占用某个对象的时候,要让它的引用计数器+1(retain ...

  9. Nginx+Keepalive实现高可用负载均衡

    1.准备2台服务器 服务器名LB1,假设IP为192.168.1.100 服务器名LB2,假设IP为192.168.1.101 2.在LB1,LB2上分别安装Nginx 步骤参照:http://blo ...

  10. windows7所有版本迅雷地址下载集合(含32位和64位) - imsoft.cnblogs

    Windows7 SP1旗舰版 32位官方原版下载: ed2k://|file|/cn_windows_7_ultimate_with_sp1_x86_dvd_618763.iso|265187737 ...