HDU-1873

使用优先队列 关键在于自定义优先级 其他没有难度

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

对于每个"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
*/
struct patient
{
    int id;//编号
    int level;//10最大
    bool operator<(const patient& p) const
    {
        if (level == p.level)
        {
            return id > p.id;//id大的话优先级小
        }
        else
            return level < p.level;//级别越高优先级越大 
    }
};

int main()
{
    int num;
    while (cin >> num)
    {
        //优先队列默认返回最大堆 模拟医生
        priority_queue<patient> A;
        priority_queue<patient> B;
        priority_queue<patient> C;
        int count = 1;
        for (int i = 0; i < num;++i)
        {
            string line;
            cin >> line;
            if (line == "IN")
            {
                int doctor;
                int level;
                cin >> doctor >> level;
                if (doctor == 1)
                {
                    A.push({count++,level});
                }
                else if (doctor == 2)
                {
                    B.push({ count++,level });
                }
                else
                {
                    C.push({ count++,level });
                }
            }
            else
            {
                int doctor;
                cin >> doctor;
                if (doctor == 1)
                {
                    if (A.empty())
                        cout << "EMPTY";
                    else {
                        cout << A.top().id;
                        A.pop();
                    }

                }
                else if (doctor == 2)
                {
                    if (B.empty())
                        cout << "EMPTY";
                    else {
                        cout << B.top().id;
                        B.pop();
                    }
                }
                else
                {
                    if (C.empty())
                        cout << "EMPTY";
                    else {
                        cout << C.top().id;
                        C.pop();
                    }
                }
                cout << endl;
            }
        }
    }
   
 
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值