hduProblem-1873优先级队列的应用

#include <bits/stdc++.h>
using namespace std;

//结构体里面的成员,在默认情况下均是 public 的权限
struct node
{
	int id;
	int pri;
	friend bool operator<(node x,node y)	//重载运算符 (friend bool operator()(node x,node y)这样不对)
	{
		if(x.pri==y.pri)
			return x.id>y.id;	//">"表示在优先级相等的情况下,越来越大排序(即小的id在前) 
		else
			return x.pri<y.pri;//"<"表示在优先级不等的情况下,越来越小排序(即大的pri在前) 
	}	
};
int n,a,b;
char str[5];
int main()
{
	while(scanf("%d",&n)!=EOF)	//等价于while(~scanf("%d",&n)
	{
		int num=1;
		priority_queue<node> doctor[4];	//类似数组doctor[0],doctor[1],doctor[2](每一个都是优先级队列容器) 
		while(n--)						//缺省了两个参数,仿函数默认用<比较排序元素 
		{
			scanf("%s",str);
			if(str[0]=='I')
			{
				scanf("%d %d",&a,&b);
				node temp;
				temp.pri=b;temp.id=num;num++;
				doctor[a].push(temp);	
			}
			else
			{
				scanf("%d",&a);
				if(!doctor[a].empty())
				{
					node temp=doctor[a].top();
					printf("%d\n",temp.id);
					doctor[a].pop();
				}
				else
				{
					puts("EMPTY");	
				}
			}
		}
	}		
	return 0;
} 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值