您的位置:首页 > 其它

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

2017-07-30 09:04 411 查看
点我看题

题意:看病排队,首先根据病情的轻重缓急来医治,严重的先治,病情相同的情况下,先来的先治,根据输入输出病人的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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: