HDU - 1873 看病要排队 (优先队列)

点我看题

题意:看病排队,首先根据病情的轻重缓急来医治,严重的先治,病情相同的情况下,先来的先治,根据输入输出病人的id.

分析:就是一个优先队列进队出队的问题,关键在于优先队列的重载小于号.

参考代码:

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

using namespace std;
int n;
struct Node{
	int id;
	int val;
	friend bool operator < ( const Node &a, const Node &b)
	{
		if( a.val != b.val)//从小到大
			return a.val < b.val;
		return a.id > b.id;//从大到小
	}
};
priority_queue<Node> q1;
priority_queue<Node> q2;
priority_queue<Node> q3;

int main()
{
	while( ~scanf("%d",&n))
	{
		while( !q1.empty())
			q1.pop();
		while( !q2.empty())
			q2.pop();
		while( !q3.empty())
			q3.pop();

		int cnt = 0;
		while( n--)
		{
			char str[10];
			int a,b;
			scanf("%s",str);
			if( !strcmp("IN",str))
			{
				cnt++;
				scanf("%d%d",&a,&b);
				Node tmp;
				tmp.id = cnt;
				tmp.val = b;
				if( a == 1)
					q1.push(tmp);	
				else if( a == 2)
					q2.push(tmp);
				else
					q3.push(tmp);
			}
			else if( !strcmp("OUT",str))
			{
				Node tmp;
				scanf("%d",&a);
				if( a == 1 && !q1.empty())
				{
					tmp = q1.top();
					printf("%d\n",tmp.id);
					q1.pop();
				}
				else if( a == 2 && !q2.empty())
				{
					tmp = q2.top();
					printf("%d\n",tmp.id);
					q2.pop();	
				}
				else if( a == 3 && !q3.empty())
				{
					tmp = q3.top();
					printf("%d\n",tmp.id);
					q3.pop();
				}
				else
					puts("EMPTY");
			}
		}
	}

	return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值