C++ priority_queue

头文件#include

基本函数

和队列基本操作相同:

top 访问队头元素
empty 队列是否为空
size 返回队列内元素个数
push 插入元素到队尾 (并排序)
emplace 原地构造一个元素并插入队列
pop 弹出队头元素
swap 交换内容

例题1:看病要排队 HDU - 1873

#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<queue>

using namespace std;

struct sick {
	int x;//序号 
	int y;//优先度 
	friend bool operator < (sick n1 , sick n2){//重写小于号 
		if(n1.y == n2.y) return n1.x > n2.x;//小于用大于号 ,相反 
		return n1.y < n2.y;
	}
};

int main(){
	int n;
	while(cin >> n){
		priority_queue<sick> doc[4];
		int id = 1;
		sick temp;
		for(int i = 0; i < n; i++ ){ 
			string str;
			int num1 , num2;
			cin >> str ;
			if(str == "IN") {
				cin >> num1 >> num2;
				temp.x = id;
				temp.y = num2;
				doc[num1].push(temp);
				id++;
			}
			else {
				cin >> num1;
				if(!doc[num1].empty()){
					cout << doc[num1].top().x << endl;
					doc[num1].pop();
				}
				else cout << "EMPTY" << endl;
				
			}
		}
		
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值