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 ...
随机推荐
- apiCloud中openFrameGroup传参
apiCloud中openFrameGroup传参 1.无效的 api.openFrameGroup({ // 打开 frame 组 name: 'group', scrollEnabled: fal ...
- Excel中将字符串中从右起第n个指定字符替换的方法
比如你想把www.baidu.com.cn中的倒数第二个”.”替换成@,则可以用: =SUBSTITUTE(A1,".","@",LEN(A1)-LEN(SUB ...
- 基于Asp.Net webApi owin oauth2的实现
干货地址:https://git.oschina.net/DpMa_/WebApi-Owin-oauth2
- 关于github里readme编辑的方法
实验室的老师昨天改完论文发我后,说按照例子改.于是才发现github里readme编辑满满的极客思维. 看了一下csdn给的教程 https://blog.csdn.net/Kaitiren/arti ...
- win10+ubuntu的坑
最近几天考虑了诸多,包括分区大小,使用烧写工具等等. 但是实际动手还是遇到了彩蛋.rufus是知乎的大神推荐的,因为UUI貌似有些版本安装时候有些问题. 而rufus的界面有诸多选项.ubuntu的图 ...
- Eclipse中將Java项目转变为Java Web项目
1.在项目上点击右键=>properties,在Project Facets配置项中,勾选Dynamic Web Module.Java.JavaScript选项. 2.用记事本打开项目目录下的 ...
- javascipt入门
一.javascript简介 javascript:(基于对象的编程语言:内部很多对象,我们只需要使用即可,几乎不需要自己创建对象) ECMAScript DOM BOM 存放位置: 建议代码放到ht ...
- h5 input失去焦点软键盘把页面顶起
var broswer=localStorage.getItem('temp') //浏览器环境 var u = navigator.userAgent var isiOS = !!u.match(/ ...
- 正则效验url
上篇文章讲到多主题的解决方案:简单暴力的TP5多主题方案 为了简化配置,所以将域名前的协议 http/https 截取了. 后台配置时就需要效验配置的格式是否正确,需要用到的正则代码如下: /*** ...
- php7 memcache和memcached.so扩展
php7安装memcache和memcached扩展 https://github.com/websupport-sk/pecl-memcache https://github.com/php-mem ...