题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=2639

Bone Collector II

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

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
 
Author
teddy
 分析:
做的第一个背包求k大值问题。。。。
参考了大佬的博客好久:
这个是HUDU2602的变形题,HDU2602题参考我的这篇博客:
原来我们要求的是最大值,现在我们要求的是第K大值
 
参加资料:
实际上,一个正确的状态转移方程的求解过程遍历了所有可用的策略,也就覆盖了问题的所有方案。只不过由于是求最优解,所以其 它在任何一个策略上达不到最优的方案都被忽略了。如果把每个状态表示成一个大小为K的数组,并在这个数组中有序的保存该状态可取到的前K个最优值。那么, 对于任两个状态的max运算等价于两个由大到小的有序队列的合并。另外还要注意题目对于“第K优解”的定义,将策略不同但权值相同的两个方案是看作同一个解还是不同的解。如果是前者,则维护有序队列时要保证队列里的数没有重复的。
 
int dp[max_v][35];//dp[j][k] 代表背包容量为j时 第k大值
 
我的理解:
普通的01背包问题求解的时候其实遍历了所有可用的策略,但是不是最优的方案就忽略了,没有记录下来,比如max/min,只取了最大或者最小值,现在我们需要用到所有的策略,那么我们就用两个数组将原来忽略的值记录下来,将两个数组合并起来(降序,注意去重),然后将合并的数组存到dp[j][x] x属于从1到k
注意点:
1.合并数组要去重,因为策略不同但是权值相同的方案是看成同一个解的
去重和排序不能采用set,不然超时。。。
 
具体参考代码:
#include<bits/stdc++.h>
using namespace std;
#define max_v 10005
int v[max_v],w[max_v];
int dp[max_v][];//dp[j][k] 代表背包容量为j时 第k大值
int a[max_v],b[max_v];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n,c,m;
scanf("%d %d %d",&n,&c,&m);
for(int i=;i<=n;i++)
{
scanf("%d",&v[i]);
}
for(int i=;i<=n;i++)
{
scanf("%d",&w[i]);
}
memset(dp,,sizeof(dp));
for(int i=;i<=n;i++)
{
for(int j=c;j>=w[i];j--)
{
int k;
// set <int,greater<int> > s;
for(k=;k<=m;k++ )
{
// a[k]=dp[j-w[i]][k]+v[i];
// b[k]=dp[j][k];
b[k]=dp[j-w[i]][k]+v[i];
a[k]=dp[j][k];
//s.insert(b[k]);
//s.insert(a[k]);
}
/* set<int,greater<int> >::iterator it;
int z=1;
for(it=s.begin();it!=s.end();it++)
{
dp[j][z++]=*it;
}
//使用set集合会超时 偷懒是不可能的了
*/ int x,y,z;
x=y=z=;
a[k]=b[k]=-;
while(z<=m&&(x<=m||y<=m))
{
if(a[x]>b[y])
{
dp[j][z]=a[x++];
}else
{
dp[j][z]=b[y++];
}
if(dp[j][z]!=dp[j][z-])
{
z++;
}
}
}
}
printf("%d\n",dp[c][m]);
}
return ;
}

HDU 2639(01背包求第K大值)的更多相关文章

  1. HDU 2639 01背包求第k大

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

  2. HDU 2639(01背包第K大)

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

  3. Bone Collector II---hdu2639(01背包求第k优解)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2639求01背包的第k大解.合并两个有序序列 选取物品i,或不选.最终的结果,是我们能在O(1)的时间内 ...

  4. 关于01背包求第k优解

    引用:http://szy961124.blog.163.com/blog/static/132346674201092775320970/ 求次优解.第K优解 对于求次优解.第K优解类的问题,如果相 ...

  5. HDU 2639 01背包(分解)

    http://acm.hdu.edu.cn/showproblem.php?pid=2639 01背包第k优解,把每次的max分步列出来即可 #include<stdio.h> #incl ...

  6. POJ2985 The k-th Largest Group[树状数组求第k大值+并查集||treap+并查集]

    The k-th Largest Group Time Limit: 2000MS   Memory Limit: 131072K Total Submissions: 8807   Accepted ...

  7. 动态求区间K大值(权值线段树)

    我们知道我们可以通过主席树来维护静态区间第K大值.我们又知道主席树满足可加性,所以我们可以用树状数组来维护主席树,树状数组的每一个节点都可以开一颗主席树,然后一起做. 我们注意到树状数组的每一棵树都和 ...

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

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

  9. HDU 1171 Big Event in HDU【01背包/求两堆数分别求和以后的差最小】

    Big Event in HDU Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) T ...

随机推荐

  1. c#如何仅在datatgirdview控件的头部(列名处)添加右键菜单

    近期在弄ArcgisAE实习,其中有一个功能是需要操作图层的属性字段的,为了方便操作图层的属性,最好是在图层的属性表中,也就是在显示图层属性的DataGirdView控件的头部添加一个右键菜单来实现相 ...

  2. Codeforces 1097 Alex and a TV Show

    传送门 除了操作 \(3\) 都可以 \(bitset\) 现在要维护 \[C_i=\sum_{gcd(j,k)=i}A_jB_k\] 类比 \(FWT\),只要求出 \(A'_i=\sum_{i|d ...

  3. linux解压tar.gz

    gnuzip或者tar -zxvf file.tar.gz unzip file.zip

  4. 自己编写jQuery插件 之 无缝滚动

    一. 效果图 二. Html骨架结构 <div class="box"> <ul> <li>1</li> <li>2&l ...

  5. opencv3.2.0图像处理之方框滤波boxFilter API函数

    /*.1.方框滤波:boxFilter函数(注:均值滤波是归一化后的方框滤波)*/ /*函数原型: void boxFilter(InputArray src, OutputArray dst, in ...

  6. Week5——Ajax

    1.简介 AJAX 相当于异步 JavaScript 和 XML,是一种用于创建快速动态网页的技术.通过在后台与服务器进行少量数据交换,AJAX 可以使网页实现异步更新.这意味着可以在不重新加载整个网 ...

  7. CCSUOJ评测系统——第二次scrum冲刺

    1.小组成员 舒 溢 许嘉荣 唐 浩 黄欣欣 廖帅元 2.第二次冲刺任务安排 对HUSTOJ数据库进行分析 序号 表名 作用 备注 1 compileinfo 记录编译错误的记录 2 contest ...

  8. Azure 和 Linux

    Azure 正在不断集结各种集成的公有云服务,包括分析.虚拟机.数据库.移动.网络.存储和 Web,因此很适合用于托管解决方案. Azure 提供可缩放的计算平台,允许即用即付,而无需投资购买本地硬件 ...

  9. 将 ExpressRoute 线路从经典部署模型转移到 Resource Manager 部署模型

    本文概述将 Azure ExpressRoute 线路从经典部署模型转移到 Azure Resource Manager 部署模型的效果. Azure 当前使用两种部署模型:Resource Mana ...

  10. 精简计算UITableView文本高度

    精简计算UITableView文本高度 本人视频教程系类   iOS中CALayer的使用 最终效果: 核心源码(计算文本高度的类) NSString+StringHeight.h 与 NSStrin ...