博客
关于我
STL & priority_queue &堆
阅读量:160 次
发布时间:2019-02-28

本文共 1351 字,大约阅读时间需要 4 分钟。

1.目前常用操作

找到数列最大或者最小的数,删除并且更新最大最小的数

2.如何定义的呢?(当然也可以手写,但是手写麻烦啊)

>  priority_queue
name;

常用操作

注意这里默认是大根堆 如果需要小根堆 插入的时候插入-x即可

heap.size();//返回heap里元素个数heap.empty();//返回heap是否为空,空则返回1,否则返回0heap.push();//插入heap.pop();//删掉heap的第一个元素heap.top();//返回heap的第一个元素

还有一些没会的操作:

priority_queue
,less
>q;priority_queue
,greater
>q;

主要运用

Huffman树(暂时没研究过Huffman树是什么)

用小根堆来做

#include 
using namespace std;typedef long long ll;int main(){ priority_queue
heap; int n; scanf("%d",&n); ll x; ll ans = 0 ; while(n -- ) { scanf("%lld",&x); heap.push(-x); } while(heap.size()>1) { int x= heap.top(); heap.pop(); int y=heap.top(); heap.pop(); ans+=-(x+y); heap.push(x+y); } printf("%lld",ans); return 0;}

#include 
using namespace std;typedef long long ll;int main(){ priority_queue
heap; int n; scanf("%d",&n); ll x; ll ans = 0 ; while(n -- ) { scanf("%lld",&x); heap.push(-x); } while(heap.size()>1) { int x= heap.top(); heap.pop(); int y=heap.top(); heap.pop(); ans+=-(x+y); heap.push(x+y); } printf("%lld",ans); return 0;}

转载地址:http://ihoc.baihongyu.com/

你可能感兴趣的文章
Mac OS 12.0.1 如何安装柯美287打印机驱动,刷卡打印
查看>>
MangoDB4.0版本的安装与配置
查看>>
Manjaro 24.1 “Xahea” 发布!具有 KDE Plasma 6.1.5、GNOME 46 和最新的内核增强功能
查看>>
mapping文件目录生成修改
查看>>
MapReduce程序依赖的jar包
查看>>
mariadb multi-source replication(mariadb多主复制)
查看>>
MariaDB的简单使用
查看>>
MaterialForm对tab页进行隐藏
查看>>
Member var and Static var.
查看>>
memcached高速缓存学习笔记001---memcached介绍和安装以及基本使用
查看>>
memcached高速缓存学习笔记003---利用JAVA程序操作memcached crud操作
查看>>
Memcached:Node.js 高性能缓存解决方案
查看>>
memcache、redis原理对比
查看>>
memset初始化高维数组为-1/0
查看>>
Metasploit CGI网关接口渗透测试实战
查看>>
Metasploit Web服务器渗透测试实战
查看>>
MFC模态对话框和非模态对话框
查看>>
Moment.js常见用法总结
查看>>
MongoDB出现Error parsing command line: unrecognised option ‘--fork‘ 的解决方法
查看>>
mxGraph改变图形大小重置overlay位置
查看>>