hdu 1873 看病要排队(优先级队列)

时间:2022-09-28 21:15:16

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1873

题目大意:

  三个医生看病,病人排队看病,病人有优先级,优先级高的提前看病,同样的优先级按先后。IN A B : A医生有B病人。OUT  A:A医生看完病人。输入看完病的病人是第几个来的。如果当前的医生没有看病人,输出“EMPYT”.

解题思路:

  三个医生队列(优先队列:可以自动排序,解决了优先级问题),定义一个病人结构体,记录病人的顺序 key 和优先级priority,如果当前病人看1号医生,则当前病人入1号医生的队列,类推。

  输出,如果1号医生输出,则取栈顶元素的key,同时出栈。如果栈顶为空,则输出“EMPTY”.

AC Code:

 #include<bits/stdc++.h>
using namespace std;
struct Patient
{
int priority,key;
friend bool operator < (Patient p1,Patient p2)
{
if(p1.priority != p2.priority)
return p1.priority < p2.priority;
else
return p1.key > p2.key;
}
};
int main()
{
int n,k,doctorId;
Patient patient[];
char type[];
while(scanf("%d",&n)!=EOF)
{
priority_queue<Patient> Doctor1;
priority_queue<Patient> Doctor2;
priority_queue<Patient> Doctor3;
k=;
while(n--)
{
scanf("%s",type);
if(strcmp(type,"IN")==)
{
patient[k].key=k;
scanf("%d %d",&doctorId,&patient[k].priority);
if(doctorId==)
Doctor1.push(patient[k]);
else if(doctorId==)
Doctor2.push(patient[k]);
else Doctor3.push(patient[k]);
k++;
}
else if(strcmp(type,"OUT")==)
{
scanf("%d",&doctorId);
if(doctorId==)
if(Doctor1.empty())printf("EMPTY\n");
else printf("%d\n",Doctor1.top().key),Doctor1.pop();
else if(doctorId==)
if(Doctor2.empty())printf("EMPTY\n");
else printf("%d\n",Doctor2.top().key),Doctor2.pop();
else if(Doctor3.empty())printf("EMPTY\n");
else printf("%d\n",Doctor3.top().key),Doctor3.pop();
}
}
}
return ;
}