[HDU] [1873] Queue Up for Treatment

Problem Description

Loading...

Problem Analysis

A simple application of the Priority Queue
priority_queue<node>q[4];
That is, build a Priority Queue of a struct type where node carries the comparison logic, and use it to simulate the process of queuing up to see a doctor.

cpp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include<stdio.h>
#include<queue>
using namespace std;
struct node {
	int num,lv;
	bool friend operator<(node a,node b) {
		if (a.lv==b.lv)
			return a.num>b.num;
		else
			return a.lv<b.lv;
	}
} a;
int main() {
	int n;
	while(scanf("%d",&n)!=EOF) {
		priority_queue<node>q[4];
		char s[5];
		int t,cnt=0;
		while(n--) {
			scanf("%s",s);
			if(s[0]=='I') {
				scanf("%d %d",&t,&a.lv);
				a.num=++cnt;
				q[t].push(a);
			} else {
				scanf("%d",&t);
				if(q[t].empty())
					printf("EMPTY\n");
				else {
					a=q[t].top();
					printf("%d\n",a.num);
					q[t].pop();
				}
			}
		}
	}
	return 0;
}

Problem link: [HDU] [1873] Queueing Up to See a Doctor

This post is licensed under CC BY-NC-SA 4.0. If it helped, or if you have anything to say, the comment box below is open.
Page views —
Unique visitors —

Loading...