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

本文介绍了一个使用C++实现的优先队列应用实例,通过重载小于号运算符来定制优先级排序规则。具体实现了病患根据级别排队就诊的场景,包括病患入队和出队的操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 }

转载于:https://www.cnblogs.com/zxotl/archive/2012/08/23/2652218.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值