跳到正文

个人记录

P3378 【模板】堆 题解

Liuyc1127约 1 分钟
原文
文章信息与版本

文章信息

发布
2022年11月19日
收录
2025年12月12日
更新
2026年11月17日
篇幅
约 1 分钟
获赞
1

版本

  1. v1当前

题目传送门
可以用优先队列来解决
《论c++有stl的好处》

C
#include <bits/stdc++.h>
using namespace std;
int main() {
	priority_queue< int,vector<int>,greater<int> >a; ////定义优先队列,升序排列(降序可以用less)
	int n;
	cin>>n;
	for(int i=1; i<=n; i++) {
		int x;
		cin>>x;
		if(x==1) {
			int y;
			cin>>y;
			a.push(y);
		}
		if(x==2)
			cout<<a.top()<<"\n"; //输出队首
		if(x==3)
			a.pop(); //删除队首
	}
	return 0;
}