我是靠谱客的博主 懦弱小鸽子,这篇文章主要介绍ACM 学习总结,现在分享给大家,希望可以做个参考。

优先队列
一个拥有权值观念的queue,自动依照元素的权值排列,权值最高排在前面。缺省情况下,priority_queue是利用一个max_heap完成的
优先队列的排序不是线性的排序,而是根据规定的优先级进行排序。内部排序是二叉树排序。
头文件: #include
定义:priority_queue <data_type> priority_queue_name;
如:priority_queue q;//默认是大顶堆
操作:
q.push(elem) 将元素elem置入优先队列
q.top() 返回优先队列的下一个元素
q.pop() 移除一个元素
q.size() 返回队列中元素的个数
q.empty() 返回优先队列是否为空

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream> #include <queue> #include <algorithm> using namespace std; #define pow2(a) ((a)*(a)) #define dist2(x, y) (pow2(x) + pow2(y)) struct coord{ int x, y; const bool operator<(const coord &b)const{ return (dist2(x, y) < dist2(b.x, b.y)); } }; int main(){ priority_queue<coord> s; coord a; a.x = 3, a.y = 2; s.push(a); a.x = 1, a.y = 2; s.push(a); a.x = 2, a.y = 2; s.push(a); cout << "Size: " << s.size() << endl; cout << "Top: " << s.top().x << ", " << s.top().y << endl; s.pop(); cout << "Top: " << s.top().x << ", " << s.top().y << endl; return 0; }

例题:丑数

最后

以上就是懦弱小鸽子最近收集整理的关于ACM 学习总结的全部内容,更多相关ACM内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(110)

评论列表共有 0 条评论

立即
投稿
返回
顶部