优先级队列 priority_queue, 巨水
本题要点:
1、定义一个结构体 person 来描述病人的信息,
int enter_id; //进场顺序
int priority; //优先级
重写运算符< , 注意,先按 优先级从小到大,在按进场顺序从大到小。
2、3个医生,每个医生前面都有一条队列。 模拟医生看病顺序。
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
const int MaxN = 2010;
int n;
char cmd[10];struct person
{
int enter_id; //进场顺序int priority; //优先级bool operator<(const person & rhs) const{
if(priority != rhs.priority)return priority < rhs.priority;return enter_id > rhs.enter_id;}
};void solve()
{
priority_queue<person> q[4];int doctor, priority_id;person per;int cnt = 0;for(int i = 0; i < n; ++i) {
scanf("%s", cmd);if(cmd[0] == 'I'){
scanf("%d%d", &doctor, &priority_id);per.enter_id = ++cnt, per.priority = priority_id;q[doctor].push(per);}else{
scanf("%d", &doctor);if(!q[doctor].size()){
printf("EMPTY\n");}else{
per = q[doctor].top();q[doctor].pop();printf("%d\n", per.enter_id);}}}
}int main()
{
while(scanf("%d", &n) != EOF){
solve();}return 0;
}/* 7 IN 1 1 IN 1 2 OUT 1 OUT 2 IN 2 1 OUT 2 OUT 1 2 IN 1 1 OUT 1 *//* 2 EMPTY 3 1 1 */