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优的更多相关文章

  1. HDU 2639 Bone Collector II(01背包变形【第K大最优解】)

    Bone Collector II Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  2. hdu 2639 Bone Collector II(01背包 第K大价值)

    Bone Collector II Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  3. 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 ...

  4. hdu 2639 Bone Collector II

    Bone Collector II Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  5. hdu 2639 Bone Collector II (01背包,求第k优解)

    这题和典型的01背包求最优解不同,是要求第k优解,所以,最直观的想法就是在01背包的基础上再增加一维表示第k大时的价值.具体思路见下面的参考链接,说的很详细 参考连接:http://laiba2004 ...

  6. [HDOJ2639]Bone Collector II(第k优01背包)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2639 题意:求01背包的第k优解 dp(i, j)表示容量为j时的i优解 对于第二维的操作和01背包几 ...

  7. 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 ...

  8. HDU 2639 Bone Collector II (01背包,第k解)

    题意: 数据是常规的01背包,但是求的不是最大容量限制下的最佳解,而是第k佳解. 思路: 有两种解法: 1)网上普遍用的O(V*K*N). 2)先用常规01背包的方法求出背包容量限制下能装的最大价值m ...

  9. HDU - 2639 Bone Collector II (01背包第k大解)

    分析 \(dp[i][j][k]\)为枚举到前i个物品,容量为j的第k大解.则每一次状态转移都要对所有解进行排序选取前第k大的解.用两个数组\(vz1[],vz2[]\)分别记录所有的选择情况,并选择 ...

随机推荐

  1. Delphi xe5 编译报environment.proj错误的解决

    Delphi xe5 在Win64位下编译报 environment.proj 错误,网上有说明: http://hi.baidu.com/fly_king1228/item/9c85fccd8db4 ...

  2. 浅尝flutter中的http请求

    import 'package:flutter/material.dart'; class News extends StatefulWidget { final String title,imgli ...

  3. linux内核中的fuse是什么?

    答: 一个用户态文件系统框架,属于内核的一种特性. 1.组成部分 fuse.ko(内核模块) + libfuse.*(用户空间库) + fusemount(挂载工具) 2.参考资料 fuse.txt

  4. Btrfs管理及应用

    一.btrfs基本概念 btrfs文件系统是2007年Oracle开发,支持GPL协议,为了取代Linux早期的ext系列文件系统. btrfs核心特性: 多物理卷支持:btrfs可由多个底层物理卷组 ...

  5. CF767C 记录错误

    链接 https://codeforces.com/contest/767/problem/C 思路 之所以把这个题放进来,是因为要记录错误 情况不止一种 所以答案存储就是>=2了 代码 #in ...

  6. nowcoder 合并回文子串

    链接:https://www.nowcoder.com/acm/contest/6/C来源:牛客网题目输入两个字符串A和B,合并成一个串C,属于A和B的字符在C中顺序保持不变.如"abc&q ...

  7. vue中使用BetterScroll

    BetterScroll可以通过给content加min-height实现永远滚动 content千万不可以删除,千万不要在 content上写v-if

  8. 使用vue做表单验证

    <template> <Form ref="formInline" :model="formInline" :rules="rule ...

  9. sublime text 中文显示异常

    本文链接:sublime text 中文显示异常 http://www.cnblogs.com/daysme/p/7549857.html 前言 sublime text 3143 用了一年时候,最近 ...

  10. sublime插件开发手记

    原:http://blog.hickwu.com/sublime插件开发手记   标题: sublime插件开发手记 时间: 2014-01-05 14:58:02 正文: 插件基本结构 基本插件实现 ...