跳到正文

题解

题解:P3378 【模板】堆

原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年4月26日
篇幅
约 2 分钟
题目
P3378

版本

  1. v1当前

这题可以用队列和动态数组

看了一下其他人的题解,发现都用的是我看不懂的二叉树,所以写个没人要的队列 思路: 根据题意,删除或输出或加入队列,直接模拟 在 a 等于1的时候将 b 插入队列 在 a 等于2的时候且队列不为空的时候输出队列的最小值 在 a 等于3的时候删除最小值 代码:

C++
#include <iostream>
#include <vector>
#include <queue>
using namespace std;

int main()
{
    int n;
    cin >> n;
    priority_queue<int, vector<int>, greater<int>> c;
    for (int i = 0; i < n; i++)
    {
        int a, b;
        cin >> a;
        if (a == 1)
        {
            cin >> b;
            c.push(b);//将b加入队列末尾
        }
        else if (a == 2)
        {
            if (!c.empty())
            {
                cout << c.top() << endl;//在不为空的时候输出最小值也就是最后一个
            }
        }
        else
        {
            if (!c.empty())
            {
                c.pop();//删除最后的的最小值
            }
        } 
    }
    return 0;
}
原文