STL 堆
测试题目:洛谷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 堆的更多相关文章
- C++ STL堆操作
/* STL 最大堆.最小堆的应用 */ #include <iostream> #include <vector> #include <algorithm> // ...
- STL 堆的使用
本来是要写leetcode上的merge k sorted lists那道题目,这个题目我还是很熟悉的,毕竟是看过算法导论的人,但是写的过程中对堆的维护代码还是挺多的,所以我想到了STL中的堆.下面就 ...
- 堆以及stl堆的使用
概念 性质: 1.堆是一颗完全二叉树,用数组实现. 2.堆中存储数据的数据是局部有序的. 最大堆:1.任意一个结点存储的值都大于或等于其任意一个子结点中存储的值. 2.根结点存储着该树 ...
- bzoj1293: [SCOI2009]生日礼物(stl堆)
1293: [SCOI2009]生日礼物 题目:传送门 题解: 据说这道题乱搞随便就水过了 本蒟蒻想到了一个用堆的水法(还专门学了学queue): 如果把每一种颜色的下一个位置都记录一下的话,一开始就 ...
- dijkstra STL 堆优化
Code: #include<iostream> #include<algorithm> #include<vector> #include<queue> ...
- 【STL学习】堆相关算法详解与C++编程实现(Heap)
转自:https://blog.csdn.net/xiajun07061225/article/details/8553808 堆简介 堆并不是STL的组件,但是经常充当着底层实现结构.比如优先级 ...
- STL 最大堆与最小堆
在第一场CCCC选拔赛上,有一关于系统调度的水题.利用优先队列很容易AC. // 由于比赛时花费了不少时间研究如何定义priority_queue的比较函数,决心把STL熟练掌握... Queue 首 ...
- 堆的基础题目学习(EPI)
堆的应用范围也比较广泛,经常游走在各种面试题目之前,不论算法设计的题目还是海量数据处理的题目,经常能看到这种数据结构的身影.堆其实就是一个完全二叉树的结构,经常利用数组来实现.包含最大堆和最小堆两种. ...
- STL之heap与优先级队列Priority Queue详解
一.heap heap并不属于STL容器组件,它分为 max heap 和min heap,在缺省情况下,max-heap是优先队列(priority queue)的底层实现机制.而这个实现机制中的m ...
随机推荐
- iOS阶段学习第33天笔记(自定义标签栏(UITabBar)介绍)
iOS学习(UI)知识点整理 一.自定义标签栏 1.方法一 单个创建标签栏 #import "AppDelegate.h" #import "SecondViewCont ...
- NPOI操作Excel辅助类
/// <summary> /// NPOI操作excel辅助类 /// </summary> public static class NPOIHelper { #region ...
- ObjectStream 及 序列化 介绍
ObjectInputStream 和 ObjectOutputStream 介绍 ObjectInputStream 和 ObjectOutputStream 的作用是,对基本数据和对象进行序列化操 ...
- 关于Java数组
今天,我们将要谈到的是Java里的数组 数组是一种容器,它是一些相同类型元素的集合.我们可以用数组,去存储一些相同类型的数据,比如,整数,浮点数,字符,...事实上,数组甚至可以用来存储同一个类的多个 ...
- jquery右键菜单
点击这里体验效果 如果要屏蔽页面原来的右键菜单,请设置disable_native_context_menu:true 以下是源代码: <!DOCTYPE html> <html&g ...
- div层调整zindex属性无效原因分析及解决方法
在做的过程中,发现了一个很简单却又很多人应该碰到的问题,设置Z-INDEX属性无效.在CSS中,只能通过代码改变层级,这个属性就是z- index,要让z-index起作用有个小小前提,就是元素的po ...
- Java基础知识点复习知识点(一)变量,流程控制,数组
- iOS之开发支付功能概述
前言:本随笔将对IOS开发的支付功能进行一个概述. 内容大纲: 一.常见的支付方案简介 二.第三方支付SDK 三.苹果官方支付方案 四.Web支付方案 正文: 一.常见的支付方案简介 在微信支付中 微 ...
- elasticsearch GIS空间查询问题解决
在GIS行业的应用越来越广泛,GIS最常用根据区域进行空间数据查询 我定义了两个方法,一起来看一下: /** * geodistance filter * 一个过滤器来过滤基于一个特定的距离从 ...
- JAVA匿名内部类
首先定义一个抽象类Computer public abstract class Computer { //抽象类是不可以常见对象的 int a=1; public abstract void onli ...