(01背包 第k优解) Bone Collector II(hdu 2639)
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.
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.
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
2
0
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
#include <map>
#include <algorithm>
using namespace std; const int N = ;
const int INF = 0x3fffffff;
const long long MOD = ;
typedef long long LL;
#define met(a,b) (memset(a,b,sizeof(a))) int dp[N][];
int a[N], b[N], c[N];
///dp[j][k] 代表容量为 j 的背包的第 k+1 优解 int cmp(int a, int b)
{
return a > b;
} int main()
{
int T;
scanf("%d", &T);
while(T--)
{
int i, j, k, n, v; scanf("%d%d%d", &n, &v, &k); met(a, );
met(b, );
met(dp, ); for(i=; i<=n; i++)
scanf("%d", &a[i]);
for(i=; i<=n; i++)
scanf("%d", &b[i]); for(i=; i<=n; i++)
{
for(j=v; j>=b[i]; j--)
{
int w = ;
for(int z=; z<k; z++) ///每次只需考虑前 k 优解的状态转换即可
{
c[w++] = dp[j][z];
c[w++] = dp[j-b[i]][z]+a[i];
} sort(c, c+w, cmp);
w = unique(c, c+w) - c;
for(int t=; t<k && t<w; t++) ///t的范围, 既不能大于 k,也不能大于 w
dp[j][t] = c[t];
}
} printf("%d\n", dp[v][k-]); }
return ;
}
(01背包 第k优解) Bone Collector II(hdu 2639)的更多相关文章
- 01背包之求第K优解——Bone Collector II
http://acm.hdu.edu.cn/showproblem.php?pid=2639 题目大意是,往背包里赛骨头,求第K优解,在普通01背包的基础上,增加一维空间,那么F[i,v,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个 然后归并排序并 ...
- 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[ ...
- Bone Collector II HDU - 2639 01背包第k最大值
题意: 01背包,找出第k最优解 题解: 对于01背包最优解我们肯定都很熟悉 第k最优解的话也就是在dp方程上加一个维度来存它的第k最优解(dp[i][j]代表,体积为i能获得的第j最大价值) 对于每 ...
- 01背包-第k优解
The title of this problem is familiar,isn't it?yeah,if you had took part in the "Rookie Cup&quo ...
- 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 ...
随机推荐
- c# 多个事件公用一个相应方法判断事件来源
假设下边的相应方法有多个事件共同使用.根据事件的sender 判断来源,做相应的处理 假设事件来源DataManSystem;private void OnSystemConnected(object ...
- 如何在Fragment中获取context
文章转载自http://blog.csdn.net/demonliuhui/article/details/51511136 这里仅供自己学习参考: Context,中文直译为“上下文”,SDK中对其 ...
- JS部分
前端三剑客(HTML,CSS,JavaScript) Html:负责一个页面的结构 Css:负责一个页面的样式 JavaScript:负责与用户进行交互 JS概念 JS是JavaScript的简称,是 ...
- Lazarus的二维码解决方案
不解释,直接上图
- C++ 的虚析构函数
当一个基类的指针指向一个派生类的对象,并用该基类的指针去删除或者析构派生类对象时,如果基类的析构函数不是声明为虚函数,那么在析构时基类的析构函数将会被直接调用,派生类的析构函数应为没被调用而导致内存泄 ...
- readv writev示例程序
当 readv() 时候,需要程序自己提供space,接收数据. #include <stdio.h> #include <stdlib.h> #include <str ...
- ApplicationContext(七)Message 源
ApplicationContext(七)Message 源 本节则是初始化消息资源池,对国际化的支持.暂时先略过. 每天用心记录一点点.内容也许不重要,但习惯很重要!
- Python之路(第五篇) Python基本数据类型集合、格式化、函数
一.变量总结 1.1 变量定义 记录某种状态或者数值,并用某个名称代表这个数值或状态. 1.2 变量在内存中的表现形式 Python 中一切皆为对象,数字是对象,列表是对象,函数也是对象,任何东西都是 ...
- PDF下载网
http://www.java1234.com/a/javabook/javaweb/2018/1103/12297.html
- Partition Equal Subset Sum
Given a non-empty array containing only positive integers, find if the array can be partitioned into ...