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

Problem 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 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
 
Author
teddy
 
Source
 题意:
有n件物品,每件物品有价值和体积,有容量为m的背包,求能够得到的第k大的价值
代码:
//以前的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优解的更多相关文章

  1. 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个 然后归并排序并 ...

  2. (01背包 第k优解) Bone Collector II(hdu 2639)

    http://acm.hdu.edu.cn/showproblem.php?pid=2639       Problem Description The title of this problem i ...

  3. HDU 3639 Bone Collector II(01背包第K优解)

    Bone Collector II Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  4. 杭电 2639 Bone Collector II【01背包第k优解】

    解题思路:对于01背包的状态转移方程式f[v]=max(f[v],f[v-c[i]+w[i]]);其实01背包记录了每一个装法的背包值,但是在01背包中我们通常求的是最优解, 即为取的是f[v],f[ ...

  5. hdu2639 01背包第K优解

    #include<iostream> #include<cstdio> #include<algorithm> #include<cstring> #i ...

  6. 01背包-第k优解

    The title of this problem is familiar,isn't it?yeah,if you had took part in the "Rookie Cup&quo ...

  7. hdu 2639 Bone Collector II (01背包,求第k优解)

    这题和典型的01背包求最优解不同,是要求第k优解,所以,最直观的想法就是在01背包的基础上再增加一维表示第k大时的价值.具体思路见下面的参考链接,说的很详细 参考连接:http://laiba2004 ...

  8. 01背包之求第K优解——Bone Collector II

    http://acm.hdu.edu.cn/showproblem.php?pid=2639 题目大意是,往背包里赛骨头,求第K优解,在普通01背包的基础上,增加一维空间,那么F[i,v,k]可以理解 ...

  9. Bone Collector II---hdu2639(01背包求第k优解)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2639求01背包的第k大解.合并两个有序序列 选取物品i,或不选.最终的结果,是我们能在O(1)的时间内 ...

随机推荐

  1. C++字符串拼接和输入

    一 .char类型字符串以空字符结尾 1.以空字符结尾,空字符被写作\0,其ASCII码为0,用来标记字符串的结尾. char dog[4]={'a','b','c','d'}   //不是一个字符串 ...

  2. day-18 滑动平均模型测试样例

    为了使训练模型在测试数据上有更好的效果,可以引入一种新的方法:滑动平均模型.通过维护一个影子变量,来代替最终训练参数,进行训练模型的验证. 在tensorflow中提供了ExponentialMovi ...

  3. Spring MVC 整合Swagger的一些问题总结

    在做Spring MVC 整合swagger的时候,遇到的两个问题: 第一个问题 在网上找了一些Spring MVC 和Swagger的例子,照着一步步的配置,结果,到最后,项目都起来了,没有任何问题 ...

  4. 小程序之web-view打开外部链接

    小程序之web-view - 传送门 web-view 组件是一个可以用来承载网页的容器,会自动铺满整个小程序页面.个人类型与海外类型的小程序暂不支持使用. 一:小程序使用web-view打开链接的前 ...

  5. vue.js学习之 如何在手机上查看vue-cli构建的项目

    vue.js学习之 如何在手机上查看vue-cli构建的项目 一:找到config文件夹下的index.js文件,打开后,将host的值改为你本地的ip,保存后重启项目 二:输入ip和端口号打开项目 ...

  6. 【IdentityServer4文档】- 支持和咨询选项

    支持和咨询选项 我们为 IdentityServer 提供多个免费和商业支持及咨询选项. 免费支持 免费支持是基于社区的,而且使用的是公共论坛 StackOverflow 有越来越多的使用 Ident ...

  7. 载入其他同名源文件导致vs编译错误

    今天下午工程编译的时候总是通不过,提示1,某个类没有某个成员,可是我去该类的头文件下查看,确实包括了这个成员啊.2,没有某个类,可是我明明定义了的. 检查了好久才发现 原来是,我打开了其他工程下的某一 ...

  8. idea超炫的自定义模板

    idea超炫的自定义模板   idea 有些快捷键 sout -> System.out.println() ,输入sout,idea能自动补全代码, 这种约定的快捷方式大大提高了效率, 而id ...

  9. 在DBGrid中可选中行而又可进入编辑状态

    如何在DBGrid中选中行,而又让它可以进入编辑状态? 也许你会问我这有什么用?呵呵,做数据库应用的兄弟们会深有感触,当用DBGrid显示的字段过多时,用户不得不拉动最下面的滚动条,去看最右边的东西, ...

  10. MATLAB中imfilter函数

    功能:对任意类型数组或多维图像进行滤波. 用法:B = imfilter(A,H) B = imfilter(A,H,option1,option2,...) 或写作g = imfilter(f, w ...