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. 字符串 date 转标准 yyyyMMdd 格式

    学习转换成数字相加的思想 public static int ToDateInt(string dateStr)        {            if (string.IsNullOrEmpt ...

  2. 《Python》 文件操作

    一.文件操作基本流程: 1.文件基本操作初识: 打开文件: 文件句柄 = open(‘文件路径’,‘编码方式’,‘打开方式’) 第一种:f = open('d:\'a.txt',encoding='u ...

  3. Zend Studio导致PHP插入数据库中文乱码【坑了个爹】

    用PHP往数据库里面插入数据,在执行INSERT语句前已经执行过 SET NAMES UTF8命令,MySql数据库的编码也确定是UTF8,然而插入中文的结果还是乱码. 找来找去,最后发现原来是用的I ...

  4. maven多环境配置

    我们在开发项目的时候,往往会有好几个环境.比如开发.预发布(测试).产品,每个环境一般用到配置都不一样,最典型的就是数据库,开发的数据库与产品的数据库肯定是不一样的,如果要多个环境的切换就得改配置,这 ...

  5. webstrom git 版本控制

    1.配置 2.用法

  6. magento模板 -- 如何安装magento模板

    在magento下面安装模板首先要了解magento的模板结构: 每个magento模板都包含如下的类似结构: --app/design/frontend/default/[模板名称] ------- ...

  7. IPM简介

    1.IPM包含3个函数. image2ground:将图像中的像素点(u, v)对应到地平面上(Z=1)IPM的像素点(x, y): ground2image:将IPM中的像素点(x, y)基于IPM ...

  8. how to check CAN frame

    1. check buffer size getsockopt(s, SOL_SOCKET, SO_SNDBUF,&snd_size, &optlen); setsockopt(s, ...

  9. ULINK2配置

    先要安装ULINK2的驱动 如果还没有检测到驱动的话,下个驱动人生,应该就行了.反正我就是这样弄成的^-^. 然后就是配置了 这样就可以下载了.

  10. poj2253 最短路

    题意:青蛙跳石头,给出石头的坐标,然后要确定一条路径,使路径上的最大跨度最小,其实也是一道最短路问题,只要将更新条件从总距离最短改为最大跨度最小就行,即从某点到当前点路径上的最大跨度如果小于当前点原本 ...