首页 > 代码库 > hdu 1873 看病要排队(优先级队列)

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

题目链接: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:

 1 #include<bits/stdc++.h> 2 using namespace std; 3 struct Patient 4 { 5     int priority,key; 6     friend bool operator < (Patient p1,Patient p2) 7     { 8         if(p1.priority != p2.priority) 9             return p1.priority < p2.priority;10         else11             return p1.key > p2.key;12     }13 };14 int main()15 {16     int n,k,doctorId;17     Patient patient[2001];18     char type[4];19     while(scanf("%d",&n)!=EOF)20     {21         priority_queue<Patient> Doctor1;22         priority_queue<Patient> Doctor2;23         priority_queue<Patient> Doctor3;24         k=1;25         while(n--)26         {27             scanf("%s",type);28             if(strcmp(type,"IN")==0)29             {30                 patient[k].key=k;31                 scanf("%d %d",&doctorId,&patient[k].priority);32                 if(doctorId==1)33                     Doctor1.push(patient[k]);34                 else if(doctorId==2)35                     Doctor2.push(patient[k]);36                 else Doctor3.push(patient[k]);37                 k++;38             }39             else if(strcmp(type,"OUT")==0)40             {41                 scanf("%d",&doctorId);42                 if(doctorId==1)43                     if(Doctor1.empty())printf("EMPTY\n");44                     else printf("%d\n",Doctor1.top().key),Doctor1.pop();45                 else if(doctorId==2)46                     if(Doctor2.empty())printf("EMPTY\n");47                     else printf("%d\n",Doctor2.top().key),Doctor2.pop();48                 else if(Doctor3.empty())printf("EMPTY\n");49                      else printf("%d\n",Doctor3.top().key),Doctor3.pop();50             }51         }52     }53     return 0;54 }

 

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