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() 返回优先队列是否为空

#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;
}

例题:丑数

猜你喜欢

转载自blog.csdn.net/weixin_43238423/article/details/88258884