测试题目:洛谷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. C#+arcengine10.0+SP5实现鹰眼(加载的是mdb数据库中的数据)

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  2. 分布式系统设计权衡之CAP

    写在最前: 1.为什么学习并记录分布式设计理念一系列相关的东西 在日常工作中系统设计评审的时候,经常会有一些同事抛出一些概念,高可用性,一致性等等字眼,他们用这些最基本的概念去反驳系统最初的设计,但是 ...

  3. disabled="true" 的标签元素不可提交

    把jsp页面的input 标签设置成不可编辑: <input  name="Id"    value="${order.Id}" readOnly=&qu ...

  4. Java中随机数的产生方式与原理

    查阅随机数相关资料,特做整理 首先说一下java中产生随机数的几种方式 在j2se中我们可以使用Math.random()方法来产生一个随机数,这个产生的随机数是0-1之间的一个double,我们可以 ...

  5. 【夯实PHP系列】PHP正则表达式

    一. 概述 1. 正则表达式的测试地址: http://tool.chinaz.com/regex/ 二.具体的常用正则表达式 1. 邮箱正则表达式: 1)\w[-\w.+]*@([A-Za-z0-9 ...

  6. CentOS 6.4 服务器版安装教程(超级详细图解)

    附:CentOS 6.4下载地址 32位:http://mirror.centos.org/centos/6.4/isos/i386/CentOS-6.4-i386-bin-DVD1to2.torre ...

  7. SQL SERVER 2008 R2数据库出现“远程过程调用失败”(0x800706be)错误,怎么办!!

    以前SQL Server 2008 不能登陆的时候,总是通过“计算机管理”→“SQL Server服务”更改一下,"SQL Server(MSSQLSERVER)". 可是现在出现 ...

  8. 使用jquery增加网站粘度

    增加网站粘度,可以在页面增加一个“随机访问”链接,当点击链接时,随机打开预先设定好的链接集合中的一个. 使用jquery可以实现这个功能,RandomVisit就是这样的一个jQuery插件. 官方网 ...

  9. jquery投色子动画

    可以点击这里体验效果:http://keleyi.com/keleyi/phtml/jqtexiao/26.htm 效果图: 代码如下: <!DOCTYPE HTML> <html& ...

  10. 关于WEB 性能优化 (摘抄)

    压缩源代码和图片 JavaScript文件源代码可以采用混淆压缩的方式,CSS文件源代码进行普通压缩,JPG图片可以根据具体质量来压缩为50%到70%,PNG可以使用一些开源压缩软件来压缩,比如24色 ...