HDU 2639 背包第k优解
Bone Collector II
Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4824 Accepted Submission(s): 2514
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.
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.
//以前的dp[V]数组再加一维dp[V]K]表示V状态时第k大的值,当枚举到第i个物品时
//dp[i][V]=max(dp[i-1][V],dp[i-1][V-v]),当前状态由两个状态转移来的所以前k大的值
//也是由两个状态的前k大的值转移来的。注意本体价值重复的算一个。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int MAXN=;
const int MAXV=;
const int MAXK=;
int dp[MAXV][MAXK];
int val[MAXN],vol[MAXN];
int N,V,K;
int main()
{
int t;
scanf("%d",&t);
while(t--){
scanf("%d%d%d",&N,&V,&K);
for(int i=;i<=N;i++)
scanf("%d",&val[i]);
for(int i=;i<=N;i++)
scanf("%d",&vol[i]);
memset(dp,,sizeof(dp));
for(int i=;i<=N;i++){
for(int j=V;j>=vol[i];j--){
int a1=,a2=,p1[],p2[];
for(int c=;c<=K;c++){
p1[c]=dp[j][c];
p2[c]=dp[j-vol[i]][c]+val[i];
}
p1[K+]=p2[K+]=-;
int c=;
while(c!=K){
int tmp=max(p1[a1],p2[a2]);
if(tmp==p1[a1]){
a1++;
if(tmp!=dp[j][c]) dp[j][++c]=tmp;
else if(tmp==) dp[j][++c]=;
}
else if(tmp==p2[a2]){
a2++;
if(tmp!=dp[j][c]) dp[j][++c]=tmp;
else if(tmp==) dp[j][++c]=;
}
}
//cout<<i<<" "<<j<<endl;
//for(int k=1;k<=K;k++) cout<<dp[j][k]<<" ";
//cout<<endl;
}
}
printf("%d\n",dp[V][K]);
}
return ;
}
HDU 2639 背包第k优解的更多相关文章
- 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个 然后归并排序并 ...
- (01背包 第k优解) Bone Collector II(hdu 2639)
http://acm.hdu.edu.cn/showproblem.php?pid=2639 Problem Description The title of this problem i ...
- HDU 3639 Bone Collector II(01背包第K优解)
Bone Collector II Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- 杭电 2639 Bone Collector II【01背包第k优解】
解题思路:对于01背包的状态转移方程式f[v]=max(f[v],f[v-c[i]+w[i]]);其实01背包记录了每一个装法的背包值,但是在01背包中我们通常求的是最优解, 即为取的是f[v],f[ ...
- hdu2639 01背包第K优解
#include<iostream> #include<cstdio> #include<algorithm> #include<cstring> #i ...
- 01背包-第k优解
The title of this problem is familiar,isn't it?yeah,if you had took part in the "Rookie Cup&quo ...
- hdu 2639 Bone Collector II (01背包,求第k优解)
这题和典型的01背包求最优解不同,是要求第k优解,所以,最直观的想法就是在01背包的基础上再增加一维表示第k大时的价值.具体思路见下面的参考链接,说的很详细 参考连接:http://laiba2004 ...
- 01背包之求第K优解——Bone Collector II
http://acm.hdu.edu.cn/showproblem.php?pid=2639 题目大意是,往背包里赛骨头,求第K优解,在普通01背包的基础上,增加一维空间,那么F[i,v,k]可以理解 ...
- Bone Collector II---hdu2639(01背包求第k优解)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2639求01背包的第k大解.合并两个有序序列 选取物品i,或不选.最终的结果,是我们能在O(1)的时间内 ...
随机推荐
- 标注点(Labeled Point)
标注点LabeledPoint是一种带有标签(Label/Response)的本地向量,它可以是稠密或者是稀疏的.在MLlib中,标注点在监督学习算法中被使用.由于标签是用双精度浮点型来存储的,故标注 ...
- appcrawler遍历工具常用方法
Usage: appcrawler [options] -a, --app <value> Android或者iOS的文件地址, 可以是网络地址, 赋值给appium的app选项 -c, ...
- leetcode个人题解——#22 Generate Parentheses
思路: 递归解决,如果左括号个数小于右括号或者左括号数小于总括号对数,则生成一个左括号,如果左括号数大于右括号,生成一个右括号. class Solution { public: vector< ...
- nodejs笔记--模块篇(三)
文件模块访问方式通过require('/文件名.后缀') require('./文件名.后缀') requrie('../文件名.后缀') 去访问,文件后缀可以省略:以"/&qu ...
- js经典试题之常用的方法
js经典试题之常用的方法 1.下面代码输出的值 let s = "bob" const replaced = s.replace('b', 'l') replaced === &q ...
- NIO 服务端TCP连接管理的方案
最近做的一个项目需要在服务端对连接端进行管理,故将方案记录于此. 方案实现的结果与背景 因为服务端与客户端实现的是长连接,所以需要对客户端的连接情况进行监控,防止无效连接占用资源. 完成类似于心跳的接 ...
- hustoj题目标准xml格式
具体格式可见google code. 分析了一下发现像<time_limit></time_limit><memory_limit></memory_limi ...
- .从列表结束中删除第N个节点
描述 给定一个链表,从列表的最后删除倒数第n个元素 例如: 给定链表:1-> 2-> 3-> 4-> 5,并且n = 2. 删除倒数第二个,链表将变为1-> 2-> ...
- linux tomcat shutdown.sh 不能正常关闭
一般造成这种原因是因为项目中有非守护线程的存在 基本原理为启动tomcat时记录启动tomcat的进程id(pid),关闭时强制杀死该进程 1.找到tomcat下bin/catalina.sh文件,v ...
- Keil MDK中Image~~RW_IRAM1~~ZI~~Limit(~表示$)
ARM程序的组成 此处所说的“ARM程序”是指在ARM系统中正在执行的程序,而非保存在ROM中的bin映像(image)文件,这一点清注意区别. 一个ARM程序包含3部分:RO, ...