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)的时间内 ...
随机推荐
- 一个简单的页面弹窗插件 jquery.pageMsgFrame.js
页面弹窗是网站中常用的交互效果,它可以强提示网站的某些信息给用户,或者作用于某些信息的修改等等功能. 这几天在做一个项目的时候,就顺捎把这个插件写一下,栽棵树,自己乘凉吧. 原创博文,转载请注明出处: ...
- MR execution in YARN
Overview YARN provides API not for application developers but for the great developers working on ne ...
- 第三周的psp
PSP: 进度条: 累计进度图: 本周PSP饼状图:
- JavaScript初探系列之基本概念
JavaScript的核心语言特性在ECMA-262中是以名为ECMAScript(ECMA, EuropeanComputer Manufacturers Association )的伪语言的形式来 ...
- 计算器软件实现系列(五)策略模式+asp.net
一 策略模式代码的编写 using System; using System.Collections.Generic; using System.Linq; using System.Web; /// ...
- iOS开发解决页面滑动返回跟scrollView左右划冲突
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithG ...
- thrift多平台安装
thrift支持多语言的RPC,一直都想深入学习了解thrift,最近有空,就上网查了些资料,学习了一下,对它的使用有了一些了解.本篇是写thrift的安装,使用方法会另起一篇来写. 本文使用thri ...
- C# WebBrowser控件详解
作者:827969653 0.常用方法 Navigate(string urlString):浏览urlString表示的网址 Navigate(System.Uri url):浏览url表 ...
- linux后台运行之screen和nohup
3.1 nohup命令 如果你正在运行一个进程,而且你觉得在退出帐户时该进程还不会结束,那么可以使用nohup命令. 该命令可以在你退出帐户/关闭终端之后继续运行相应的进程. nohup就是不挂起的意 ...
- linq的decimal类型保存到数据库只保存到小数点后两位的问题
今天的一个decimal类型保存到数据的问题困扰了我很长时间,最后就是一个小小的设置问题解决······坑······深坑···· 话不多说,直接说问题,在说答案: 问题:linq当采用EF的DbCo ...