看病要排队(优先队列)

看病要排队

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

思路

使用集合,集合中每个元素都是一个优先队列,用来表示对应医生的诊治队列,优先级高则先诊治,相同则按照先来后到的顺序诊治。

Coding
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

/**
 * @ClassName: QueuingToSeeDoctor
 * @Description: 看病要排队
 * @Author syk
 * @Date 2021/12/4
 * @Version 1.0
 */
public class QueuingToSeeDoctor {

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine().strip());
        Comparator<node> cmp = (o1, o2) -> o2.getLevel() - o1.getLevel();
        List<Queue<node>> pqs = new ArrayList<>(4);
        pqs.add(new PriorityQueue<>(cmp));
        pqs.add(new PriorityQueue<>(cmp));
        pqs.add(new PriorityQueue<>(cmp));
        pqs.add(new PriorityQueue<>(cmp));
        int id = 0, num = 0;
        String[] results = new String[n];
        for (int i = 0; i < n; i++) {
            String[] strings = br.readLine().split(" ");
            if ("IN".equals(strings[0])) {
                String doctor = strings[1];
                String level = strings[2];

                if ("1".equals(doctor)) {
                    pqs.get(0).add(new node(++id, Integer.parseInt(level)));
                } else if ("2".equals(doctor)) {
                    pqs.get(1).add(new node(++id, Integer.parseInt(level)));
                } else if ("3".equals(doctor)) {
                    pqs.get(2).add(new node(++id, Integer.parseInt(level)));
                }

            } else {
                char target = strings[1].charAt(0);
                int index = target - '0';
                Queue<node> pq = pqs.get(index - 1);
                if (pq.isEmpty()) {
                    results[num++] = "EMPTY";
                } else {
                    results[num++] = "" + pq.poll().getId();
                }
            }
        }
        for (String result : results) {
            if (result == null) {
                break;
            }
            System.out.println(result);
        }

    }

}


class node {
    private int id;
    private int level;

    node(int id, int level) {
        this.id = id;
        this.level = level;
    }

    public int getId() {
        return id;
    }

    public int getLevel() {
        return level;
    }

}

运行效果

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

隔壁老康

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值