hdu-2639 Bone Collector II 背包第K优
http://acm.hdu.edu.cn/showproblem.php?pid=2639
在背包的基础上维护一个size<=K的最大值集合,为什么维护K个就好了呢,因为如果当前状态有多余K个最优解,前K个就足够转移到下一状态并占满前K了,所以K个之后的都没必要维护。
#include <iostream>
#include <cstdio>
#include <vector>
#include <string>
#include <map>
#include <queue>
#include <set>
#define LL long long
using namespace std;
const LL N=;
set<LL> dp[N];
LL w[N];
LL v[N];
LL n,V,k;
int main()
{
cin.sync_with_stdio(false);
int t;
cin>>t;
while(t--)
{
cin>>n>>V>>k;
for(int i=; i<=V; i++)dp[i].clear(),dp[i].insert();
for(int i=; i<n; i++)
cin>>v[i];
for(int i=; i<n; i++)
cin>>w[i];
for(int i=; i<n; i++)
{
for(int j=V; j-w[i]>=; j--)
{
int e=j-w[i];
for(set<LL>::iterator it=dp[e].begin();it!=dp[e].end();it++)
{
LL now=*it+v[i];
if(dp[j].size()==k)
{
if(now>*(dp[j].begin()))
dp[j].erase(*dp[j].begin()),dp[j].insert(now);
}
else dp[j].insert(now);
}
}
}
if(dp[V].size()<k)cout<<<<endl;
else
cout<<*dp[V].begin()<<endl;
}
return ;
}
hdu-2639 Bone Collector II 背包第K优的更多相关文章
- HDU 2639 Bone Collector II(01背包变形【第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大价值)
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
Bone Collector II Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- hdu 2639 Bone Collector II (01背包,求第k优解)
这题和典型的01背包求最优解不同,是要求第k优解,所以,最直观的想法就是在01背包的基础上再增加一维表示第k大时的价值.具体思路见下面的参考链接,说的很详细 参考连接:http://laiba2004 ...
- [HDOJ2639]Bone Collector II(第k优01背包)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2639 题意:求01背包的第k优解 dp(i, j)表示容量为j时的i优解 对于第二维的操作和01背包几 ...
- HDU 2639 Bone Collector II (dp)
题目链接 Problem Description The title of this problem is familiar,isn't it?yeah,if you had took part in ...
- HDU 2639 Bone Collector II (01背包,第k解)
题意: 数据是常规的01背包,但是求的不是最大容量限制下的最佳解,而是第k佳解. 思路: 有两种解法: 1)网上普遍用的O(V*K*N). 2)先用常规01背包的方法求出背包容量限制下能装的最大价值m ...
- HDU - 2639 Bone Collector II (01背包第k大解)
分析 \(dp[i][j][k]\)为枚举到前i个物品,容量为j的第k大解.则每一次状态转移都要对所有解进行排序选取前第k大的解.用两个数组\(vz1[],vz2[]\)分别记录所有的选择情况,并选择 ...
随机推荐
- LOIC
Pre: http://sourceforge.net/projects/loic Getting the Software To DDos, first your going to have to ...
- Bootstrap3基础 form-control 圆角的输入框,光标放入后边框变色
内容 参数 OS Windows 10 x64 browser Firefox 65.0.2 framework Bootstrap 3.3.7 editor ...
- 枚举+排序|神奇算式|2014年蓝桥杯A组题解析第三题-fishers
标题:神奇算式 由4个不同的数字,组成的一个乘法算式,它们的乘积仍然由这4个数字组成. 比如: 210 x 6 = 1260 8 x 473 = 3784 27 x 81 = 2187 都符合要求. ...
- (转)renren-fast解读(一)
(二期)8.renren-fast项目解读(一) [课程八]预防xss...注入.xmind0.2MB [课程八预习]开...解读.xmind0.5MB 课程八_日志处理与...模块.xmind0.2 ...
- 【入门】Gradle的基本使用、在IDEA中的配置、常用命令
一.介绍 java的源码构建工具,大致经历了 ant -> maven -> gradle 这个过程,每一次进步,都是在解决之前的工具所带来的问题,简单来说: 1. ant 功能虽然也很强 ...
- facebook api之Ads Insights API
The Ads Insights API provides API access for reporting and analytics purposes. When exclusively usin ...
- HANA 与 Oracle 12c哪一个更快
本文来自于对Which is Faster HANA or Oracle 12C?的阅读笔记,列举了其中的要点.并不代表我的观点. 简介 SAP声称HANA是最快的数据库,因此SAP的新ERP系统S4 ...
- git pull 提示 There is no tracking information for the current branch
在执行git pull的时候,提示当前branch没有跟踪信息: git pull There is no tracking information for the current branch. P ...
- ubuntu 14.04 安装pip
sudo apt-get install python-pip
- 关于python的面向对象
一,面向对象 1..面向对象的过程:一切以事物的流程为核心,核心是过程二字,过程是指解决问题的步骤, 是一种机械是的编程思维 优点:负责的问题流程化,编写相对简单 缺点:可扩展性能差 2.面向对象一切 ...