8.1.5 看病要排队

看病要排队

Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 43 Accepted Submission(s): 31

Problem Description
看病要排队这个是地球人都知道的常识。
不过经过细心的0068的观察,他发现了医院里排队还是有讲究的。0068所去的医院有三个 医生(汗,这么少)同时看病。而看病的人病情有轻重,所以不能根据简单的先来先服务的原则。所以医院对每种病情规定了10种不同的优先级。级别为10的优 先权最高,级别为1的优先权最低。医生在看病时,则会在他的队伍里面选择一个优先权最高的人进行诊治。如果遇到两个优先权一样的病人的话,则选择最早来排 队的病人。

现在就请你帮助医院模拟这个看病过程。
 

Input
输入数据包含多组测试,请处理到文件结束。
每组数据第一行有一个正整数N(0<N<2000)表示发生事件的数目。
接下来有N行分别表示发生的事件。
一共有两种事件:
1:"IN A B",表示有一个拥有优先级B的病人要求医生A诊治。(0<A<=3,0<B<=10)
2:"OUT A",表示医生A进行了一次诊治,诊治完毕后,病人出院。(0<A<=3)
 

Output
对于每个"OUT A"事件,请在一行里面输出被诊治人的编号ID。如果该事件时无病人需要诊治,则输出"EMPTY"。
诊治人的编号ID的定义为:在一组测试中,"IN A B"事件发生第K次时,进来的病人ID即为K。从1开始编号。
 

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

优先队列

  1 #include <cmath>
  2 #include <cstdio>
  3 #include <algorithm>
  4 #include <cstring>
  5 #include <string>
  6 #include <cstdlib>
  7 #include <queue>
  8 using namespace std;
  9 
 10 const int maxn=210;
 11 struct qq
 12 {
 13     int t,x;
 14     friend bool operator < (qq a,qq b)
 15     {
 16         if (a.x==b.x)
 17             return a.t>b.t;
 18         return a.x<b.x;
 19     }
 20 } ya;
 21 priority_queue<qq> q1,q2,q3;
 22 int a,b,x,cnt,n;
 23 char s[20];
 24 
 25 void close()
 26 {
 27 exit(0);
 28 }
 29 
 30 void in()
 31 {
 32     scanf("%d %d",&a,&b);
 33     cnt++;
 34     ya.x=b; ya.t=cnt;
 35     if (a==1)
 36         q1.push(ya);
 37     if (a==2)
 38         q2.push(ya);
 39     if (a==3)
 40         q3.push(ya);
 41 }
 42 
 43 void out()
 44 {
 45     scanf("%d",&a);
 46     if (a==1)
 47     {
 48         if (q1.empty())
 49         {
 50             printf("EMPTY
");
 51             return;
 52         }
 53         ya=q1.top();
 54         q1.pop();
 55     }
 56     if (a==2)
 57     {
 58         if (q2.empty())
 59         {
 60             printf("EMPTY
");
 61             return;
 62         }
 63         ya=q2.top();
 64         q2.pop();
 65     }
 66     if (a==3)
 67     {
 68         if (q3.empty())
 69         {
 70             printf("EMPTY
");
 71             return;
 72         }
 73         ya=q3.top();
 74         q3.pop();
 75     }
 76     printf("%d
",ya.t);
 77 }
 78 
 79 void init()
 80 {
 81     while(scanf("%d",&n)!=EOF)
 82     {
 83         while (!q1.empty()) q1.pop();
 84         while (!q2.empty()) q2.pop();
 85         while (!q3.empty()) q3.pop();
 86         cnt=0;
 87         if (n==0) break;
 88         for (int i=1;i<=n;i++)
 89         {
 90             scanf("%s",s);
 91             if (s[0]=='I')
 92                 in();
 93             else
 94                 out();
 95         }
 96     }
 97 }
 98 
 99 int main ()
100 {
101     init();
102     close();
103     return 0;
104 }
原文地址:https://www.cnblogs.com/cssystem/p/3212487.html