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#版本与vs的对应关系

    版本 .NET Framework版本 Visual Studio版本 发布日期 特性 C# 1.0 .NET Framework 1.0 Visual Studio .NET 2002 2002.1 ...

  2. 迁移HTML5移动项目到PhoneGap

    MyEclipse开年钜惠 在线购买低至75折!立即开抢>> [MyEclipse最新版下载] 一.创建一个新的PhoneGap应用程序项目 PhoneGap应用程序项目的结构与HTML5 ...

  3. 一张图让你学会Python

    有编程基础的人一看就可以了解 Python 的用法了.真正的 30 分钟上手.国外一高手画的,现把它翻译成中文,入门超简单python入门神图 *单击放大

  4. 解决Ubuntu下adb无法联接手机终端

    1.首先确认开发者选项中USB调试是否打开. 2.在终端输入lsusb,如下图: 查看设备是否已经连接,如果没有此选项,请检查你的手机数据线是否正常连接,否则你的手机就可以用来防身和砸核桃了. 3.在 ...

  5. HOG+SVM+INRIAPerson数据集代码

    #include <iostream> #include <opencv2/core/core.hpp> #include <opencv2/highgui/highgu ...

  6. Python 操作Excel之通过xlutils实现在保留原格式的情况下追加写入数据

    在Python操作Excel 的模块有 xlrd.xlwt.xlutils等. xlrd:读取Excel文件数据 xlwt:写入Excel 数据,缺点是Excel格式无法复用,为了方便用户,写入的话, ...

  7. JQuery实现高级检索功能

    https://blog.csdn.net/muziruoyi/article/details/44494465 < div id= "0" class ="row ...

  8. java导出Excel 好文收藏

    http://www.cnblogs.com/Damon-Luo/p/5919656.html https://www.cnblogs.com/klguang/p/6425422.html

  9. 【java编程】正确重写hashCode和equesl方案

    一. 正确书写hashCode的办法: [原则]按照equals( )中比较两个对象是否一致的条件用到的属性来重写hashCode(). {1}. 常用的办法就是利用涉及到的的属性进行线性组合. {2 ...

  10. 【maven】使用import scope解决maven继承(单)问题

    测试环境 maven 3.3.9 想必大家在做SpringBoot应用的时候,都会有如下代码: <parent> <groupId>org.springframework.bo ...