测试题目:洛谷P3378 【模板】堆

插入,删除,取最小


方法0:STL 优先队列

1198ms

#include<iostream>
#include<cstdio>
#include<queue>
#include<vector>
using namespace std;
inline int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-; c=getchar();}
while(c>=''&&c<=''){x=x*+c-''; c=getchar();}
return x*f;
}
int n,op;
priority_queue<int, vector<int>, greater<int> > q;
int main(){
//freopen("in.txt","r",stdin);
n=read();
for(int i=;i<=n;i++){
op=read();
if(op==) q.push(read());
else if(op==) printf("%d\n",q.top());
else q.pop();
}
}

方法1:algorithm库 heap系列函数

520ms 这个数字.....

make_heap(begin,end,cmp) 建堆 前闭后开    cmp定义<运算,可选 注意同样是默认大根堆

push_heap(begin,end,cmp) 插入 前闭后开 插入最后一个元素

pop_heap(begin,end,cmp) 删除 把堆顶元素放到最后一个位置

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long ll;
const int N=1e6+;
inline int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x*f;
}
int n,op,x;
int a[N],len=;
inline bool cmp(int a,int b){return a>b;}
int main(){
n=read();
for(int i=;i<=n;i++){
op=read();
if(op==){
a[++len]=read();
push_heap(a+,a++len,cmp);
}else if(op==) printf("%d\n",a[]);
else pop_heap(a+,a++len,cmp),len--;
}
}

方法2:pb_ds库

据说竞赛可用

#include <ext/pb_ds/priority_queue.hpp>

using namespace __gnu_pbds;

支持配对堆(pairing_heap)、二叉堆(binary_heap)、二项堆(binomial_heap)、冗余计数二项堆(redundant-counter binomial_heap,没找到通用译名,故自行翻译)、经改良的斐波那契堆(thin_heap)

使用方法:__gnu_pbds::priority_queue<int,greater<int>,pairing_heap_tag> q; 第三个参数换成想用的名称就行了,默认配对堆

支持join操作,然而本文不考虑

pairing_heap_tag 428ms

binomial_heap 544ms

rc_binomial_heap 610ms

thin_heap_tag 790ms

结合WC课件中的测试,用默认的pairing就好了

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <ext/pb_ds/priority_queue.hpp>
using namespace std;
using namespace __gnu_pbds;
inline int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x*f;
}
int n,op,x;
__gnu_pbds::priority_queue<int,greater<int> > q;
int main(){
n=read();
for(int i=;i<=n;i++){
op=read();
if(op==) q.push(read());
else if(op==) printf("%d\n",q.top());
else q.pop();
}
}

STL 堆的更多相关文章

  1. C++ STL堆操作

    /* STL 最大堆.最小堆的应用 */ #include <iostream> #include <vector> #include <algorithm> // ...

  2. STL 堆的使用

    本来是要写leetcode上的merge k sorted lists那道题目,这个题目我还是很熟悉的,毕竟是看过算法导论的人,但是写的过程中对堆的维护代码还是挺多的,所以我想到了STL中的堆.下面就 ...

  3. 堆以及stl堆的使用

    概念 性质: 1.堆是一颗完全二叉树,用数组实现.    2.堆中存储数据的数据是局部有序的. 最大堆:1.任意一个结点存储的值都大于或等于其任意一个子结点中存储的值.      2.根结点存储着该树 ...

  4. bzoj1293: [SCOI2009]生日礼物(stl堆)

    1293: [SCOI2009]生日礼物 题目:传送门 题解: 据说这道题乱搞随便就水过了 本蒟蒻想到了一个用堆的水法(还专门学了学queue): 如果把每一种颜色的下一个位置都记录一下的话,一开始就 ...

  5. dijkstra STL 堆优化

    Code: #include<iostream> #include<algorithm> #include<vector> #include<queue> ...

  6. 【STL学习】堆相关算法详解与C++编程实现(Heap)

    转自:https://blog.csdn.net/xiajun07061225/article/details/8553808 堆简介   堆并不是STL的组件,但是经常充当着底层实现结构.比如优先级 ...

  7. STL 最大堆与最小堆

    在第一场CCCC选拔赛上,有一关于系统调度的水题.利用优先队列很容易AC. // 由于比赛时花费了不少时间研究如何定义priority_queue的比较函数,决心把STL熟练掌握... Queue 首 ...

  8. 堆的基础题目学习(EPI)

    堆的应用范围也比较广泛,经常游走在各种面试题目之前,不论算法设计的题目还是海量数据处理的题目,经常能看到这种数据结构的身影.堆其实就是一个完全二叉树的结构,经常利用数组来实现.包含最大堆和最小堆两种. ...

  9. STL之heap与优先级队列Priority Queue详解

    一.heap heap并不属于STL容器组件,它分为 max heap 和min heap,在缺省情况下,max-heap是优先队列(priority queue)的底层实现机制.而这个实现机制中的m ...

随机推荐

  1. luogg_java学习_07_抽象类_接口_多态学习总结

    这篇博客总结了半天,希望自己以后返回来看的时候理解更深刻,也希望可以起到帮助初学者的作用. 转载请注明 出自 : luogg的博客园 , 抽象 一种专门用来做父类,被继承的. (模板) 格式: abs ...

  2. Guava学习-目录

    备份一下地址: 目录 1. 基本工具 [Basic utilities] 让使用Java语言变得更舒适 1.1 使用和避免null:null是模棱两可的,会引起令人困惑的错误,有些时候它让人很不舒服. ...

  3. HTML思维导图

  4. ["1", "2", "3"].map(parseInt)?

    ["1", "2", "3"].map(parseInt)得到什么? 答案是:[1, NaN, NaN]. 原因:parseInt接收的是两 ...

  5. artTemplate模板引擎学习实战

    在我的一篇关于智能搜索框异步加载数据的文章中,有博友给我留言,认为我手写字符串拼接效率过低,容易出错.在经过一段时间的摸索和学习之后,发现现在拼接字符串的方法都不在是自己去书写了,而是使用Javasc ...

  6. iOS 怎么设置 UITabBarController 的第n个item为第一响应者?

    iOS 怎么设置 UITabBarController 的第n个item为第一响应者? UITabBarController 里面有个属性:selectedIndex @property(nonato ...

  7. Android编码规范04

    private final String MESSAGE_WARN = "您输入的密码有误,请重新输入!"; private final String CLASS_ONE = &q ...

  8. IOS RunLoop浅析 一

    RunLoop犹如其名循环. RunLoop 中有多重模式. 在一个“时刻”只能值执行一种模式. 因此在使用RunLoop时要注意所实现的效果有可能不是你想要的. 在这里用NSTimer展示一下Run ...

  9. 《java JDK7 学习笔记》之接口与多态

    1.对于"定义行为"也就是接口,可以使用interface关键字定义,接口中的方法不能操作,直接标示为abstract,而且一定是public修饰的. 类要操作接口,必须使用imp ...

  10. Oracle数据库迁移

    1 在数据迁移时,用户首先有权限修改数据库,并且进行表空间创建.删除等权利 例如: select * from dba_tab_privs where grantee='SCOT'; ---查看SCO ...