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 ...
随机推荐
- 神经网络预测mnist时候如果不归一化,则准确率仅仅10%下文作者svm也遇到了。
转自:http://blog.csdn.net/jeryjeryjery/article/details/72649320 这两天用Python来实现手写数字识别,刚开始用原始数据进行训练,结果预测结 ...
- POJ 2190 模拟
按照题意模拟就好- 注意"X"只能出现在最后一位... // by SiriusRen #include <cstdio> using namespace std; c ...
- 此请求的查询字符串的长度超过配置的 maxQueryStringLength 值
异常详细信息: System.Web.HttpException: 此请求的查询字符串的长度超过配置的maxQueryStringLength 值. 我碰到此问题出现的原因是重写了HttpModule ...
- 如何让iframe背景色透明框架页文件设置
如何让iframe背景色透明框架页文件设置:<body style="background-color:transparent" > 或 <body bgColo ...
- TP框架传值
/*TP框架传值*/ location.href = "../add/add/department/"+department+"/username/"+user ...
- 织梦dedecms支持flash的flv文件播放功能代码
1.打开/include/FCKeditor/editor/dialog/dede_media.htm if(playtype=="rm"|| (playtype=="- ...
- NOIp2018模拟赛三十八
爆〇啦~ A题C题不会写,B题头铁写正解: 随手过拍很自信,出分一看挂成零. 若要问我为什么?gtmdsubtask! 神tm就一个subtask要么0分要么100,结果我预处理少了一点当场去世 难受 ...
- data structure alignment(数据对齐)
概述: 数据对齐指数据在计算机内存中排放和获取的方式.包含三个方面:数据对齐(data alignment).数据结构填充(data alignment).打包(packing) 如果数据是自然对齐的 ...
- /etc/rsyncd.conf
[root@backup ~]# cat /etc/rsyncd.conf #Rsync server#created by oldboy ##rsyncd.conf start##uid = rsy ...
- PHP JWT初识
一直没有好好看过jwt,直到前两天要做web验证,朋友给我推荐了jwt.才发现jwt已经被大家广泛的应用了.看来我有点out了.哈哈,趁着这个世界来好好看看这个. JWT(JSON Web Token ...