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)的时间内 ...
随机推荐
- 2018Java研发实习内推
作者:sdu王镜鑫链接:https://www.nowcoder.com/discuss/74573?type=0&order=4&pos=7&page=1来源:牛客网 本人某 ...
- 《剑指Offer》题三十一~题四十
三十一.栈的压入.弹出序列 题目:输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否为该栈的弹出顺序.假设压入栈的数字均不相等.例如,序列{1, 2, 3, 4 ,5}是某栈的压栈序列 ...
- 软件工程-东北师大站-第二次作业psp
1.本周PSP 2.本周进度条 3.本周累计进度图 代码累计折线图 博文字数累计折线图 本周PSP饼状图
- Notes of the scrum meeting before publishing(12.17)
meeting time:18:30~20:30p.m.,December 17th,2013 meeting place:3号公寓一层 attendees: 顾育豪 ...
- 什么是Processing
Processing是一种计算机语言,以JAVA语法为基础,可转化成JAVA程序,不过在语法上简易许多.所有的原始代码及开发环境开放,主要用于艺术.影像.影音的设计与处理. 其次为什么要介绍这款软件呢 ...
- (一)Model的产生及处理
MVC的概念其实最早可以追溯到很久很久以前,并不是WEB开发过程中所首创, 但是,MVC也适合WEB上的开发,并真正的在WEB开发领域广泛应用.MVC的第一个字母M是Model,承载着View层和Co ...
- Alpha 冲刺报告(4/10)
Alpha 冲刺报告(4/10) 队名:洛基小队 峻雄(组长) 已完成:继续行动脚本的编写 明日计划:尽量完成角色的移动 剩余任务:物品背包交互代码 困难:具体编码进展比较缓慢 ----------- ...
- linux下安装多个jdk版本的切换问题
下载地址: https://www.azul.com/downloads/zulu/ 解压: [root@localhost java]# tar -zxvf /usr/java/zulu8.38.0 ...
- python爬虫--打开爬取页面
def requests_view(response): import webbrowser requests_url = response.url base_url = '<head>& ...
- CCleaner专业版注册码
下载软件安装之后: 1.断网 2.用户名:任意,注册码:C2YW-XZT7-A4SE-UD89-YZPC