01背包--hdu2639
hdu-2639
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
题目大意
有n块骨头,V容量的背包,把骨头放进包里,求价值第k大时是多少。
思路
就是个01背包的变种,根据dp思想,在01背包的基础上加多一个维度,dp[j][k]表示容量为j的背包下,第k大的价值。
首先考虑第1大的数,是max(dp[i][j], dp[i - 1][j - c[i]] + w[i])
可以推断,第k大的值可以在两组数dp[i][j][z]、dp[i - 1][j - c[i]][z] + w[i],z∈[1, ... , k]中得到
然而并不能直接知道这两组数中前k大的数,所以将dp[i][j][z]放入A[],将dp[i - 1][j - c[i]][z] + w[i]放入B[]
然后将A[]、B[]两组数一起排序,就能得到第k大的数
代码
#include <iostream>
#include <cstring>
#include <string>
#include <vector>
#include <set>
#include <algorithm>
#include <cmath>
#include <cstdio>
using namespace std;
typedef long long LL;
const int N = 1005;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
int main()
{
int T;
cin >> T;
while(T--)
{
LL n, V, K;
cin >> n >> V >> K;
LL w[N], c[N];
for(int i = 1;i <= n;++i)
cin >> w[i];
for(int i = 1;i <= n;++i)
cin >> c[i];
LL dp[N][35];
memset(dp, 0, sizeof(dp));
for(int i = 1;i <= n;++i)
{
for(int j = V;j >= c[i];--j)
{
LL A[35], B[35];
int a, b, num;
for(int k = 1;k <= K;++k)
{
A[k] = dp[j - c[i]][k] + w[i];
B[k] = dp[j][k];
}
A[K + 1] = B[K + 1] = -1;//-1 < 0。a <= K做判断条件会出错
a = b = num = 1;
while(num <= K && (A[a] != -1 || B[b] != -1))
{
if(A[a] > B[b])
dp[j][num] = A[a++];
else
dp[j][num] = B[b++];
if(dp[j][num] != dp[j][num - 1])
num++;
}
}
}
cout << dp[V][K] << endl;
}
return 0;
}
01背包--hdu2639的更多相关文章
- dp之01背包hdu2639(第k优解)
http://acm.hdu.edu.cn/showproblem.php?pid=2639 题意:给出一行价值,一行体积,让你在v体积的范围内找出第k大的值.......(注意,不要 和它的第一题混 ...
- HDU2639(01背包第K大)
Bone Collector II Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- HDU--2639 Bone Collector II(01背包)
题目http://acm.hdu.edu.cn/showproblem.php?pid=2639 分析:这是求第K大的01背包问题,很经典.dp[j][k]为背包里面装j容量时候的第K大的价值. 从普 ...
- NO11——01背包
# include <stdio.h> # include <stdlib.h> # include <string.h> # define max(x,y) x& ...
- 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 ...
- UVALive 4870 Roller Coaster --01背包
题意:过山车有n个区域,一个人有两个值F,D,在每个区域有两种选择: 1.睁眼: F += f[i], D += d[i] 2.闭眼: F = F , D -= K 问在D小于等于一定限度的时 ...
- POJ1112 Team Them Up![二分图染色 补图 01背包]
Team Them Up! Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 7608 Accepted: 2041 S ...
- Codeforces 2016 ACM Amman Collegiate Programming Contest A. Coins(动态规划/01背包变形)
传送门 Description Hasan and Bahosain want to buy a new video game, they want to share the expenses. Ha ...
- 51nod1085(01背包)
题目链接: http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1085 题意: 中文题诶~ 思路: 01背包模板题. 用dp[ ...
随机推荐
- 使用Intellij中的Spring Initializr来快速构建Spring Boot/Cloud工程
在之前的所有Spring Boot和Spring Cloud相关博文中,都会涉及Spring Boot工程的创建.而创建的方式多种多样,我们可以通过Maven来手工构建或是通过脚手架等方式快速搭建,也 ...
- 改善C#公共程序类库质量的10种方法(转)
出处:http://www.cnblogs.com/JamesLi2015/p/3140897.html 最近重构一套代码,运用以下几种方法,供参考. 1 公共方法尽可能的使用缓存 public s ...
- C++11学习笔记之三lamda表达式,std::function, std::bind
//lamda //first lamda [] {}; // second lamda []() //or no need () when paramater is null { std::cout ...
- 【转载】mysql中timestamp,datetime,int类型的区别与优劣
转载来自souldak,微博:@evagle以下内容整合筛选自互联网: int1. 占用4个字节2. 建立索引之后,查询速度快3. 条件范围搜索可以使用使用between4. 不能使用mysql提供的 ...
- mvc中使用Hangfire处理后台任务
考虑下如下代码,在数据保存后,需要发送邮件,发送邮件是个耗时的工作. 我们的目的是,数据保存成功后,就可以返回响应了,发送邮件不重要,不需要等待邮件发送成功 [HttpPost] public Act ...
- 【Head First Java 读书笔记】(八)接口与抽象类
接口是什么?它是一种百分之百纯抽象的类. 什么是抽象类?即无法初始化的类. 例如,我们设计一个animal类,以此类为父类,分别设计了多种动物子类,例如Lion,Tiger,Cat,Wolf,Do ...
- 【小梅哥SOPC学习笔记】切换NIOS II CPU的主内存后软件中需要注意的几点设置
切换NIOS II CPU的主内存后软件中需要注意的几点设置 有时候,我们可能面对这样一种情况: 1. 我们创建一个SOPC系统,并在QSYS中设置NIOS II的复位地址和异常地址都指向SRAM: ...
- HBASE的优化、hadoop通用优化,Linux优化,zookeeper优化,基础优化
HBase 的优化3.1.高可用在 HBase 中 Hmaster 负责监控 RegionServer 的生命周期,均衡 RegionServer 的负载,如果Hmaster 挂掉了,那么整个 HBa ...
- 一类适合初学者的DP:最大子段和与最大子矩阵
最近在水简单DP题,遇到了两道层层递进的DP题,于是记录一下 一.最大子段和 题意: 给出一个长度为n(n<=1e5)的序列,求连续子段的最大值 比如说2 3 -4 5 的最大值是6 而 2 ...
- vs项目属性中的包含目录和库目录以及附加依赖项全都配置正确了,却还是提示:无法解析的外部符号
这种情况下,很大可能是lib文件有问题 我是用vs编译下载的源代码文件得到的lib出现了如题的情况, 后来去网站上直接下载了lib文件,竟然解决了!-.-