STL之优先队列
来源:互联网 发布:淘宝浏览量有什么用 编辑:程序博客网 时间:2024/05/01 17:05
stl是一个c++中很好的一个功能,但是stl如果使用不当也会酿成大错,而且stl还十分的慢,如果不开O2优化,会比手打慢了好几倍。虽说如此,但stl还是深受c++程序员喜爱,今天我们就来讲讲STL中的优先队列:priority_pueue。
首先来讲讲他的定义方式:
最简单的定义方式:priorithy_pueue <变量的类型> q;
注意这是大根堆,如果要定义一个小根堆的话要这么定义: priorithy_pueue <变量的类型,vector<变量的类型,greadter<变量的类型> > q或者写一个cmp函数。其实还是很简单的。
再来讲讲优先队列的几个常用的函数:
函数名 功能 时间复杂度
size 返回堆的大小 O(1)
top 返回堆的最大值或最小值 O(1)
push 在堆中插入一个值 O(log(n))
pop 删除堆的最大值或最小值 O(log(n))
empty 判断堆是否为空 O(1)
现在来看一到经典的题目:合并果子
自己手写堆的方法:
#include<cstdio>#include<cstdlib>#include<iostream>#include<cstring>using namespace std;int a[1000001],head=0;void push(int k,int x) { if(x<a[k>>1]) { int y=a[k>>1]; a[k>>1]=x; a[k]=y; push(k>>1,x); }}void pop(int k) { if(k*2>head||k*2+1>head) return ; if((a[(k<<1)+1]<a[k<<1]||a[k<<1]>a[k])&&a[k*2+1]<a[k]) { int y=a[(k<<1)+1]; a[(k<<1)+1]=a[k]; a[k]=y; pop((k<<1)+1); } else if(a[k]>a[k<<1]) { int y=a[k<<1]; a[k<<1]=a[k]; a[k]=y; pop(k<<1); }}int top() { return a[1];}int main() { memset(a,0xfffffff,sizeof(a)); int n,k,l,js=0; cin>>n; for(int i=1; i<=n; i++) { head++; cin>>a[head]; if(head!=1) push(head,a[head]); } for(int i=1; i<n; i++) { k=top(); a[1]=a[head]; pop(1); head--; l=top(); a[1]=a[head]; pop(1); head--; js+=k+l; head++; a[head]=k+l; if(head!=1) push(head,k+l); } cout<<js; return 0;}
用STL的方法:
#include<cstdio>#include<cstdlib>#include<iostream>#include<queue>using namespace std;priority_queue <int,vector<int>,greater<int> > a;int main() { int n,k,l,js=0; cin>>n; for(int i=1; i<=n; i++) { cin>>k; a.push(k); } for(int i=1; i<n; i++) { k=a.top(); a.pop(); l=a.top(); a.pop(); js+=k+l; a.push(k+l); } cout<<js; return 0;}
阅读全文
0 0
- STL之优先队列
- STL之优先队列
- STL之优先队列
- stl 之 优先队列
- STL之优先队列
- STL之优先队列
- STL之优先队列
- STL之优先队列
- STL之优先队列
- STL容器之优先队列
- STL容器之优先队列
- STL容器之优先队列
- STL具体操作之优先队列
- STL容器之优先队列
- STL 之 优先队列(priority_queue)
- STL容器之优先队列
- C++STL之优先队列
- STL容器之优先队列
- java--静态绑定和动态绑定
- Hive实战之Youtube数据集
- mysql索引总结----mysql 索引类型以及创建
- 在fragmnet中获取父activity为null
- Java基础----抽象类
- STL之优先队列
- C语言中的static 详细分析
- hdu1394
- spring的事务控制
- Http请求头和响应头
- Python3批量移动指定文件到指定文件夹
- 信用卡评分模型优化
- Windows 10 安装 Cuda, Tensorflow, 和keras,附CPU/GPU切换小技巧
- Android26-ContentProvider