Bone Collector II

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

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]为容量为j的背包所获得的第k大价值。在01背包中 状态转移方程为 dp[j]=max(dp[j],dp[j-w[i]])+v[i],这个求的是第1大。我们用dp[j][1...k]表示第1大到第k大。
那么dp[j][1]=max_1th(dp[j][1],dp[j-w[i]]+v[i]),dp[j][2]=max_2th(dp[j][1],dp[j-w[i]][1],dp[j][2],dp[j-w[i]][2]+v[i])( 注意:不是dp[j][2]=max(dp[j][2],dp[j-w[i][2]+v[i]) )
dp[j][k]=max_kth(dp[j][1],...,dp[j][k],dp[j-w[i]][1]+v[i],...,dp[j-w[i]][k]+v[i])。
/*
Accepted 2639 858MS 5372K 831 B G++
*/
#include"cstdio"
#include"cstring"
#include"algorithm"
using namespace std;
const int MAXN=;
int dp[MAXN][MAXN];
int n,W,K;
int v[MAXN],w[MAXN];
int vec[MAXN],cnt;
bool comp(int x,int y)
{
return x > y;
}
void KthZeroOnePack()
{
for(int i=;i<n;i++)
{
for(int j=W;j>=w[i];j--)
{
cnt=;
for(int th=;th<=K;th++)
{
vec[cnt++]=dp[j][th];
vec[cnt++]=dp[j-w[i]][th]+v[i];
}
sort(vec,vec+cnt,comp);
cnt=unique(vec,vec+cnt)-vec;
for(int th=;th<=min(cnt,K);th++) dp[j][th]=vec[th-];
}
}
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d%d",&n,&W,&K);
memset(dp,,sizeof(dp));
for(int i=;i<n;i++) scanf("%d",&v[i]);
for(int i=;i<n;i++) scanf("%d",&w[i]);
KthZeroOnePack();
printf("%d\n",dp[W][K]);
}
return ;
}

上面用了STL里的sort函数速度较慢...

因为dp[j][1]...dp[j][k]与dp[j-w[i]][1]+v[i]...dp[j-w[i]][k]+v[i]是依次递减的,那么我们可以用两个数组将这两组数组保存起来,再O(N)的时间内求得第K大。

/*
Accepted 2639 171MS 5372K 966 B G++
*/
#include"cstdio"
#include"cstring"
#include"algorithm"
#include"queue"
using namespace std;
const int MAXN=;
int dp[MAXN][MAXN];
int n,W,K;
int v[MAXN],w[MAXN];
int s1[MAXN],s2[MAXN];
void KthZeroOnePack()
{
for(int i=;i<n;i++)
{
for(int j=W;j>=w[i];j--)
{
for(int th=;th<=K;th++)
{
s1[th-]=dp[j][th];
s2[th-]=dp[j-w[i]][th]+v[i];
}
s1[K]=s2[K]=-;
int cnt=;
int cnt1=,cnt2=;
while(cnt<=K&&(s1[cnt1]!=-||s2[cnt2]!=-))
{
if(s1[cnt1]>s2[cnt2]) dp[j][cnt]=s1[cnt1++];
else dp[j][cnt]=s2[cnt2++];
if(dp[j][cnt]!=dp[j][cnt-]) cnt++;
}
}
}
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d%d",&n,&W,&K);
memset(dp,,sizeof(dp));
for(int i=;i<n;i++) scanf("%d",&v[i]);
for(int i=;i<n;i++) scanf("%d",&w[i]);
KthZeroOnePack();
printf("%d\n",dp[W][K]);
}
return ;
}

HDU2639(01背包第K大)的更多相关文章

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

  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(01背包第K大)

    http://acm.hdu.edu.cn/showproblem.php?pid=2639 http://blog.csdn.net/lulipeng_cpp/article/details/758 ...

  4. hdu2639 01背包第K优解

    #include<iostream> #include<cstdio> #include<algorithm> #include<cstring> #i ...

  5. HDU 2639 (01背包第k优解)

    /* 01背包第k优解问题 f[i][j][k] 前i个物品体积为j的第k优解 对于每次的ij状态 记下之前的两种状态 i-1 j-w[i] (选i) i-1 j (不选i) 分别k个 然后归并排序并 ...

  6. HDU2639[背包第K大]

    题目链接[http://acm.hdu.edu.cn/showproblem.php?pid=2639] 题意:求第k大背包. 题解:利用二路归并的思想,求解第K大的值. #include<bi ...

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

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

  8. Bone Collector II HDU - 2639 01背包第k最大值

    题意: 01背包,找出第k最优解 题解: 对于01背包最优解我们肯定都很熟悉 第k最优解的话也就是在dp方程上加一个维度来存它的第k最优解(dp[i][j]代表,体积为i能获得的第j最大价值) 对于每 ...

  9. HDU 3639 Bone Collector II(01背包第K优解)

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

随机推荐

  1. 向odoo贡献中文翻译

    建议通过 osc-git向odoo贡献中文翻译     osc-git 是指'开源中国'的git平台. 网址是 http://git.oschina.net/     注册osc-git 账号省略. ...

  2. mysql: 关于MySQL InnoDB锁行还是锁表?

          baidu zone - 关于MYSQL Innodb 锁行还是锁表,深入讲解

  3. ElasticSearchserver操作命令

    在win7环境,进入elasticsearch安装文件夹的bin文件夹: 1. elasticsearch.bat 就能够启动elasticsearch了.执行这个插件的优点是:elasticsear ...

  4. 今天遇到一个git码云同步的问题

    一开始是因为eclipse编码不同意导致乱码,所以我和师弟就想统一都用UTF-8的,师弟统一好了,让我pull一下,pull的时候有冲突,因为我和师弟都修改了其中一个文件,然后我这边就删除了那个文件再 ...

  5. 安卓UI适配限定符

    引言 对于程序在不同尺寸的Android机器上执行,对UI的适用性造成了额外的开销,只是限定符的出现,非常方便的攻克了这个问题.通过创建限定符相关的文件夹来解决资源的载入. 限定符用处 限定符(mdp ...

  6. ScrollView滑动的监听

    ScrollView滑动的监听 有时候我们须要监听ScrollView的滑动事件.来完毕业务需求. 第一种: 能够直接实现OnTouchListener接口.在这里面写你所须要的操作 scrollVi ...

  7. [oracle]pl/sql --分页过程demo

    这句sql能够用来查询一张表中的特定位置的记录 --查询的方法获取分页的语句 select *from (select t1.*,rownum rn from (select *from books) ...

  8. 针对基于Phison(群联)U盘的BadUSB攻击

    修改U盘固件使之在插入电脑时能执行键盘指令.原文和源码在此,粗略翻译了一下.https://github.com/adamcaudill/Psychson 其实还有类似的成品卖,叫做USB Rubbe ...

  9. Javascript MVC 学习笔记(二) 控制器和状态

    今天进入第二个部分:控制器. 控制器和状态 从以往的开发经验来看.我们都是将状态保存在server的session或者本地cookie中,但Javascript应用往往被限制在单页面,所以我们也能够将 ...

  10. 功能强大的Xcode辅助工具Faux Pas:帮你找到各种隐形的bug

    本文转载至 http://www.cocoachina.com/industry/20140804/9307.html Faux Pas(Beta版下载地址)是一个Xcode辅助工具,用以检查Xcod ...