【C++编程基础】AOJ (C++ Programming II)—2-C-priority queue

2-C-queue

题目

priority queue
Priority queue is a container of elements which the element with the highest priority should be extracted first. For n priority queues Qi (i=0,1,…,n−1) of integers, perform a sequence of the following operations.
insert(t, x): Insert x to Qt.
getMax(t): Report the maximum value in Qt. If Qt is empty, do nothing.
deleteMax(t): Delete the maximum element from Qt. If Qt is empty, do nothing.
In the initial state, all stacks are empty.
输入
The input is given in the following format.
n q
query1
query2
:
queryq
Each query queryi is given by 0 t x or 1 t or 2 t where the first digits 0, 1 and 2 represent insert, getMax and deleteMax operations respectively. 1≤n≤1,000 1≤q≤200,000 −1,000,000,000≤x≤1,000,000,000
输出
For each getMax operation, print an integer in a line.
样例输入
2 10
0 0 3
0 0 9
0 0 1
1 0
2 0
1 0
0 0 4
1 0
0 1 8
1 1
样例输出
9
3
4
8

题解(代码流程)

#include<bits/stdc++.h>

using namespace std;

int main() {
   ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int n, q;
    cin >> n >> q;
    vector<priority_queue<int>> Q(n);//定义优先队列
    while (q--) {
        int p;
        cin >> p;
        if (p == 0) {
            int t, x;
            cin >> t >> x;
            Q[t].push(x);
        } else if (p == 1) {
            int t;
            cin >> t;
            if (!Q[t].empty())
                cout << Q[t].top() << endl;
        } else if (p == 2) {
            int t;
            cin >> t;
            if (!Q[t].empty()) Q[t].pop();
        }
    }

    return 0;
}
priority_queue<int,vector,greater> QQ;//默认是大顶堆less,声明小顶堆greater。

小结

学会定义优先队列,对队列中的元素进行修改
remember
c++ Priority Queue 优先队列类似队列,但是在这个数据结构中的元素按照一定的断言排列有序。
  • empty()
  • pop()
  • push()
  • size()
  • top()
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值