01背包-第k优解
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 2 31).
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
- #include <iostream>
- #include <cstdio>
- using namespace std;
- #define max(a,b) ((a)>(b)?(a):(b))
- const int maxn = ;
- int main()
- {
- int T;
- scanf("%d", &T);
- int dp[maxn][], val[maxn], vol[maxn], A[], B[];
- while (T--)
- {
- int n, v, k;
- scanf("%d %d %d", &n, &v, &k);
- int i, j, kk;
- for (i=; i<n; i++) scanf("%d", &val[i]);
- for (i=; i<n; i++) scanf("%d", &vol[i]);
- memset(dp, , sizeof(dp));
- int a, b, c;
- for (i=; i<n; i++)
- for (j=v; j>=vol[i]; j--)
- {
- for (kk=; kk<=k; kk++)
- {
- A[kk] = dp[j-vol[i]][kk] + val[i];
- B[kk] = dp[j][kk];
- }
- A[kk] = -, B[kk] = -;
- a = b = c = ;
- while (c<=k && (A[a] != - || B[b] != -))
- {
- if (A[a] > B[b])
- dp[j][c] = A[a++];
- else
- dp[j][c] = B[b++];
- if (dp[j][c] != dp[j][c-])
- c++;
- }
- }
- printf("%d\n", dp[v][k]);
- }
- return ;
- }
01背包-第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 ...
- HDU2639Bone Collector II[01背包第k优值]
Bone Collector II Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- HDU 2639 背包第k优解
Bone Collector II Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- 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 ...
- hdu 2639 Bone Collector II (01背包,求第k优解)
这题和典型的01背包求最优解不同,是要求第k优解,所以,最直观的想法就是在01背包的基础上再增加一维表示第k大时的价值.具体思路见下面的参考链接,说的很详细 参考连接:http://laiba2004 ...
随机推荐
- zzulioj--1707--丧心病狂的计数(水题)
1707: 丧心病狂的计数 Time Limit: 1 Sec Memory Limit: 128 MB Submit: 237 Solved: 105 SubmitStatusWeb Board ...
- Another app is currently holding the yum lock; waiting for it to exit…
yum被锁定无法使用,错误信息截图如下:解决方法:rm -rf /var/run/yum.pid 来强行解除锁定,然后你的yum就可以运行了
- 升级Ubuntu18.04后遇到的坑
升级过程: 直接do-release-update 就可以直接从16.04更新到18.04了. 中间会提升更新一些配置文件, 我大部分都选择了N. 然后就成功升级到18.04了, 显卡驱动什么的都 ...
- http协议无状态中的 "状态" 到底指的是什么?!(转载)
转载自:https://www.cnblogs.com/bellkosmos/p/5237146.html 引子: 最近在好好了解http,发现对介绍http的第一句话[http协议是无状态的,无 ...
- Hbase项目(完整版)
涉及概念梳理:命名空间 4.1.1.命名空间的结构 1) Table:表,所有的表都是命名空间的成员,即表必属于某个命名空间,如果没有指定,则在default默认的命名空间中. 2) RegionSe ...
- mybatis与spring整合配置
mybatis与spring整合配置: 第一种方式:(此处配置扫描的包路径.注解.每个mapper类上面需要加@Repository才能纳入spring的bean管理器中) <!-- 自动扫描m ...
- HTML基础——网站首页显示页面
1.表格标签: border设置边框,align设置位置(居中等),bgcolor设置背景颜色,cellspacing设置边框之间的空隙,cellpadding设置边框与里面内容的间距. table表 ...
- DotNetCore.1.0.1-VS2015Tools.Preview2.0.2 安装错误分析及解决办法(so far)
折腾了这么多天总算弄完了,真恶心.为了让其他童靴避免掉进我遇到的坑里,我决定把最近遇到问题及其解决办法总结一下,希望对大家有帮助. 1.对于2016年7月底以前安装VS用户来说,可能不会那么迫切安装这 ...
- 使用3ds Max制作卡通狗教程
使用软件::3ds Max 软件下载:http://www.xy3dsmax.com/xiazai.html 全教程完,学完记得交作业.如果本教程对您有所帮助,请推荐给你的朋友. 全教程完,学完记得交 ...
- vector ----- size函数注意事项
vector 的size函数返回vector大小,返回值类型为size_type,Member type size_type is an unsigned integral type,即无符号整数: ...