Description
看病要排队这个是地球人都知道的常识。
不过经过细心的0068的观察,他发现了医院里排队还是有讲究的。0068所去的医院有三个医生(汗,这么少)同时看病。而看病的人病情有轻重,所以不能根据简单的先来先服务的原则。所以医院对每种病情规定了10种不同的优先级。级别为10的优先权最高,级别为1的优先权最低。医生在看病时,则会在他的队伍里面选择一个优先权最高的人进行诊治。如果遇到两个优先权一样的病人的话,则选择最早来排队的病人。
现在就请你帮助医院模拟这个看病过程。
不过经过细心的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)
每组数据第一行有一个正整数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开始编号。
诊治人的编号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
sort的应用;
1、可以传入两个参数;
sort(a,a+N) ,其中a是数组,a+N表示对a[0]至a[N-1]的N个数进行排序(默认从小到大排序);
2、传入三个参数;
sort(a,a+N,cmp),第三个参数是一个函数 ;
如果让函数从大到小排序,可以用如下算法实现;
bool cmp(int a,int b){return a>b};
sort(A,A+N,cmp);
而stable_sort的用法与sort一致,区别是stable_sort函数遇到两个数相等时,不对其交换顺序;这个应用在数组里面不受影响,当函数参数传入的是结构体时,会发现两者之间的明显区别;
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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
struct node
{
int id;
int grade;
};
typedef vector<node> doclist;
doclist list[4];
int cmp(node x,node y)
{
return x.grade>y.grade;
}
int main()
{
int n;
while(cin>>n)
{
int i;
for(i=1;i<=3;i++)
list[i].clear();
string type;int docnum,grade;
int count=0;
node temp;
while(n--)
{
cin>>type;
if(type=="IN")
{
cin>>docnum>>grade;
count++;
temp.id=count,temp.grade=grade;
list[docnum].push_back(temp);
stable_sort(list[docnum].begin(),list[docnum].end(),cmp);
}
else
{
cin>>docnum;
if(list[docnum].empty())cout<<"EMPTY"<<endl;
else
{
cout<<list[docnum].begin()->id<<endl;
doclist::iterator it= list[docnum].begin();
list[docnum].erase(it);
}
}
}
}
return 0;
}
优先队列:
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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
struct node {
int id;
int p;
bool operator < (const node &a) const {
if(a.p == p) return a.id < id;
else {
return a.p > p;
}
}
};
priority_queue<node> Q1, Q2, Q3;
char mark[5];
int index = 1;
void init() {
index = 1;
memset(mark, '0', sizeof(mark));
while(!Q1.empty()) Q1.pop();
while(!Q2.empty()) Q2.pop();
while(!Q3.empty()) Q3.pop();
}
int main()
{
int n;
int p, d;/// priority, doctor;
node tmp;
while(scanf("%d", &n) != EOF) {
init();
for(int i = 0; i < n; i++) {
scanf("%s", mark);
if(mark[0]=='I') {
scanf("%d%d", &d, &p);
tmp.id = index++;
tmp.p = p;
if(d==1) {
Q1.push(tmp);
continue;
}
if(d==2) {
Q2.push(tmp);
continue;
}
if(d==3){
Q3.push(tmp);
continue;
}
}
else {
int res; ///result
bool flag = false; ///is queue empty?
scanf("%d", &d);
if(d==1) {
if(!Q1.empty()) {
flag = true;
tmp = Q1.top();
Q1.pop();
res = tmp.id;
}
}
if(d==2) {
if(!Q2.empty()) {
flag = true;
tmp = Q2.top();
Q2.pop();
res = tmp.id;
}
}
if(d==3) {
if(!Q3.empty()) {
flag = true;
tmp = Q3.top();
Q3.pop();
res = tmp.id;
}
}
if(flag) {
cout << res << endl;
}
else {
cout << "EMPTY" << endl;
}
}
}
init();
}
return 0;
}