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

http://acm.hdu.edu.cn/showproblem.php?pid=1873

主要练习优先队列的应用,学会如何重载小于号(operator <)

若用自定义类型应用优先队列时要重载小于号,类比sort函数   

View Code
 1 #include <iostream>
 2 #include <queue>
 3 #include <string>
 4 using namespace std;
 5 struct FUN
 6 {
 7     int ID;
 8     int lv;
 9     friend bool operator < (const FUN &x,const FUN &y)
10     {
11         if(x.lv == y.lv)
12         {
13             return x.ID > y.ID;
14         }
15         return x.lv < y.lv;
16     }
17 };
18 int main()
19 {
20     int n;
21     while(cin>>n)
22     {
23         int ID = 1;
24         priority_queue <FUN> doc[4];
25         FUN sick = {0};
26         for(int i=0;i<n;i++)
27         {
28             string str;
29             cin>>str;
30             int num;
31             if(str == "IN")
32             {
33                 cin>>num;
34                 sick.ID = ID;
35                 cin>>sick.lv;
36                 doc[num].push(sick);
37                 ID++;
38             }
39             else
40             {
41                 cin>>num;
42                 if(!doc[num].empty())
43                 {
44                     cout<<doc[num].top().ID<<endl;
45                     doc[num].pop();
46                 }
47                 else
48                 {
49                     cout<<"EMPTY"<<endl;
50                 }
51             }
52         }
53     }
54     return 0;
55 }
posted @ 2012-08-23 12:06  zx雄  阅读(451)  评论(0编辑  收藏  举报