ACM-Hdu-看病要排队(STL优先队列)

    此题是有坑的,具体体现在以下方面:

                           1.    一个有三个医生, 需要建立三个 优先队列

                           2.   结构体排序的比较函数需要特别的注意(写法不会的话,以后我会补上 语法讲解)

                           

#include <cstdio>
#include <queue>
#include <cstring>
using namespace std;
/*
   优先队列: q.top() --> 在队列中查找优先权最大的元素 有限队列插入和删除都是 O(lgn)
   优先级最高的在最前面
   */

struct Node    //此题的坑在于————————> 排序的稳定性,如果两个数一样,不能交换
{
        int priority;
        int index;
        bool operator<(const Node &b) const
        {
                if ( priority != b.priority)//不等于就倒退 
                        return priority < b.priority;
                return index > b.index;
        }
};


int main()
{
        int n;
        while( ~scanf("%d", &n) )   
        {
                priority_queue<Node> que[4];//系统默认元素值大的优先级高
                int ccccc = 1;
                for(int i = 0; i < n; ++i)
                {
                        char str[10];
                        scanf("%s", str);

                        if( !strcmp(str, "IN"))
                        {
                                Node temp;  
                                temp.index = ccccc++;//记录这个病人的索引  

                                int doctor_index;//病人要看的医生的索引  
                                int priority;//这个病人的优先级  
                                scanf("%d%d",&doctor_index,&priority);  
                                temp.priority = priority;  

                                que[doctor_index].push(temp); 
                        }

                        else if( !strcmp(str, "OUT"))
                        {
                                int doctor_index;
                                scanf("%d", &doctor_index);
                                if( que[doctor_index].empty() == false )
                                {
                                        printf("%d\n", que[doctor_index].top().index);
                                        que[doctor_index].pop();
                                }
                                else
                                        printf("EMPTY\n");
                        }
                }
        }
        return  0;
}

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值