【HDU-2639】Bone Collector II(背包)
来源:互联网 发布:历史大事年表知乎 编辑:程序博客网 时间:2024/05/17 08:39
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 2 31).
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
题目大意
Bone Collector的升级版,求第K大价值。
思路
基本思路不变,还是01背包问题,但是不是求背包的最大价值而是求第K大价值,可以用set,队列来维护第K大值,也可以简单的用两个数组d1,d2来记录每一个值,通过比较d1,d2的值更新dp数组,这里dp数组要开二维(注意这不是二维费用的背包问题,不要混淆)即dp[maxn][31],第二维用来表示第几大的价值。
Bone Collector的状态转移方程是:
dp[j]=max(dp[j],dp[j-w[i]]+v[i]);
但是这题不是,而是改用一个从1~k的循环来求每一个费用下面放和不放第i的骨头的价值,即:
for(l=1;l<=k;l++){ d1[l]=dp[j][l];//不放的情况 d2[l]=dp[j-w[i]][l]+v[i];//放的情况}
接下来只要根据大小合并d1,d2就行了。这样dp[wei][k]就是背包长度为wei下第K大的价值了
代码
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int maxn=100000+5;int dp[maxn][31],v[maxn],n,k,w[maxn],wei;int d1[maxn],d2[maxn];int main(){ int t; scanf("%d",&t); while(t--) { memset(dp,0,sizeof(dp)); scanf("%d %d %d",&n,&wei,&k); for(int i=1;i<=n;i++) { scanf("%d",&v[i]); } for(int i=1;i<=n;i++) { scanf("%d",&w[i]); } for(int i=1;i<=n;i++) { for(int j=wei;j>=w[i];j--) { int l; for(l=1;l<=k;l++) { d1[l]=dp[j][l]; d2[l]=dp[j-w[i]][l]+v[i]; } d1[l]=d2[l]=-1; int x,y,z; x=y=z=1; while(z<=k&&(d1[x]!=-1||d2[y]!=-1)) { if(d1[x]>d2[y]) { dp[j][z]=d1[x++]; } else { dp[j][z]=d2[y++]; } if(dp[j][z-1]!=dp[j][z]) z++; } } } printf("%d\n",dp[wei][k]); } return 0;}
- 【HDU-2639】Bone Collector II(背包)
- HDU 2639 - Bone Collector II(01背包)
- HDU--2639--Bone Collector II--01背包
- hdu 2639 Bone Collector II(第K大背包)
- HDU 2639 Bone Collector II (背包 第K优解)
- hdu 2639 Bone Collector II (01背包)
- hdu 2639 Bone Collector II(第K优背包)
- Hdu 2639 Bone Collector II (01背包第k优解)
- hdu 2639 Bone Collector II (0/1背包)
- hdu 2639Bone Collector II 01背包第k大问题
- HDU 2639 Bone Collector II(DP:01背包)
- HDU 2639 Bone Collector II(01背包变型)
- Bone Collector II - HDU 2639 背包第k优解
- HDU 2639 Bone Collector II 背包k优解
- hdu 2639 Bone Collector II 01背包第K优解
- HDU 2639 Bone Collector II 01背包第K优解
- hdu oj 2639 Bone Collector II (01背包k优解)
- HDU - 2639 Bone Collector II(第K大01背包)
- 主流WiFi芯片与性能简介
- Word自动编号多级标题设置
- 枚举
- 在DLL编程中,导出函数为什么需要extern "C"
- 如何构建研发质量管理体系?
- 【HDU-2639】Bone Collector II(背包)
- Caffe for Windows 训练cifar10 VS2013
- iOS pushnotification
- 如何正确使用C++多重继承
- 使用CMD命令提示符配置IP
- 几种常见的过程模型
- vmware 12 虚拟机安装mac osx 10.11.3 分辨率设置
- 卡拉OK效果的实现-IOS音乐播放器
- 体验Impress.js