问题链接:HDU1873 看病要排队
Problem Description 看病要排队这个是地球人都知道的常识。
Input 输入数据包含多组测试,请处理到文件结束。
Output 对于每个"OUT A"事件,请在一行里面输出被诊治人的编号ID。如果该事件时无病人需要诊治,则输出"EMPTY"。
Sample Input 7 IN 1 1 IN 1 2 OUT 1 OUT 2 IN 2 1 OUT 2 OUT 1 2 IN 1 1 OUT 1
Sample Output 2 EMPTY 3 1 1
Author linle
Source |
问题分析:这是一道模拟题,用优先队列解决。三个医生分别对应一个优先队列数组q[]的各个元素。
#include <iostream>
#include <queue>
#include <string>
using namespace std;
struct node{
int p;//优先级
int num;//序号
friend bool operator<(node a,node b)
{
if(a.p!=b.p)
{
return a.p<b.p;
}
else
{
return a.num>b.num;
}
}
};
int main()
{
int n;
while(cin>>n)
{
priority_queue<node> q[3];
string s;
int a,b;
node nn;
int cnt=1;
for(int i=1;i<=n;i++)
{
cin>>s;
if(s=="IN")
{
cin>>a>>b;
nn={b,cnt++};
q[a-1].push(nn);
}
else if(s=="OUT")
{
cin>>a;
if(q[a-1].empty())
{
cout << "EMPTY" << endl;
}
else
{
nn=q[a-1].top();
q[a-1].pop();
cout<<nn.num<<endl;
}
}
}
}
return 0;
}