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 ...
随机推荐
- String转换成int型
private boolean judge(String str){ int year = 0; try{ year = Integer.valueOf(str).intValue(); }catch ...
- 跨域调用接口——WebClient通过get和post请求api
AJAX不可以实现跨域请求,经过特殊处理才行.一般后台可以通过WebClient实现跨域请求~ //get 请求 string url = string.Format("htt ...
- JS循环 for while 全局/局部变量 短路
循环语句: For for循环的格式 for(var i = 0; i < 10; i ++){ } for循环的执行顺序: ① ② 若判断为 true 进④ 进③ 进②判断 ……循环 ...
- vmware workstation中的NAT配置
宿主机:win10: IP:192.168.1.101 GW:192.168.1.1 以太网2(VMNET8) IP:192.168.100.1 GW:nonevmware中的虚拟网络设置(NAT): ...
- Session与Token认证机制 前后端分离下如何登录
字号 1 Web登录涉及到知识点 1.1 HTTP无状态性 HTTP是无状态的,一次请求结束,连接断开,下次服务器再收到请求,它就不知道这个请求是哪个用户发过来的.当然它知道是哪个客户端地址发过来的 ...
- 微信小程序手势滑动卡片案例
最近工作中有项目要使用微信小程序技术进行开发,其中一项功能困扰了我很久,卡片滑动动效以及手势识别.经过一番研究和参考,现在把成果展示.记录自己踩到的坑,如果大家有需要,也可以帮助到大家. 效果图: 首 ...
- centos7-centos6常用配置对比
设置(CentOS 6 vs CentOS 7)系统常用配置 ysvinit vs Upstart vs Systemd) 常见设置: 字符集CentOS 6方法:/etc/sysconfig/i1 ...
- pandas 4 处理缺失数据nan
from __future__ import print_function import pandas as pd import numpy as np np.random.seed(1) dates ...
- 怎么给Unity写一个原生的插件
本文章由cartzhang编写,转载请注明出处. 所有权利保留. 文章链接:http://blog.csdn.net/cartzhang/article/details/50266889 作者:car ...
- C语言修改文件某部分内容
两种方法 1.全部读入内存 修改后重新存入文件 2.边读边写到另一新建文件 要修改的部分修改后存入新建文件 其他部分原封不动写入 写完删掉原先文件 将这个新的改为删掉那个的名字 方法一 读入内存修改 ...