[HDU1837]看病要排队




看病要排队这个是地球人都知道的常识。
不过经过细心的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

这道题我相信很多人都是用优先队列ac的,我就立了个flag,用队列可以完成;

于是在全机房的反对下我撸了一个支持上浮操作的队列

每进来一个病人,就按照他的优先集在队列里进行上浮操作,复杂度o(n2)但是常数小

那我就放代码了

#include<iostream>
#include<queue>
using namespace std;
int tot;
struct peitient{
	int priority;
	int num;
};
struct node_doctor{
	peitient que[2001];
	int head;
	int tail;
	int size;
}doctor[4];
void push(int A,int B)
{
	int head=++doctor[A].head;
	doctor[A].que[head].num=++tot;
	doctor[A].que[head].priority=B;
	doctor[A].size++;
	void up(int) ;
	up(A);//上浮
}
int pop(int A)
{
	int tail=doctor[A].tail;
	if(doctor[A].size)
	{
			
		cout<<doctor[A].que[tail].num<<endl;
		doctor[A].tail++;
		doctor[A].size--;
	}
	else
	{
		cout<<"EMPTY"<<endl;
	}
}
void up(int A)
{
	int now=doctor[A].head;
	while(doctor[A].que[now].priority>doctor[A].que[now-1].priority&&now>doctor[A].tail)
	{
		swap(doctor[A].que[now],doctor[A].que[now-1]);//类似于冒泡
		now--;
	}
}
void search(int A)//我调试用的,输出医生A的队列
{
	for(int i=doctor[A].tail;i<=doctor[A].head;i++)
	{
		cout<<doctor[A].que[i].num<<" "<<doctor[A].que[i].priority<<endl;
	}
	cout<<endl;
}
void ini()
{
	for(int i=1;i<=3;i++)
	{
		doctor[i].tail=1;
		doctor[i].head=0;
		doctor[i].size=0;
	}
}
int main()
{
	int N;
	while(cin>>N)
	{
		string str;
		tot=0;
		ini();
		for(int i=1;i<=N;i++)
		{
			cin>>str;
			if(str=="IN")
			{
				int A,B;
				cin>>A>>B;
				push(A,B);
				//search(A);
			}
			else
			{
				int A;
				cin>>A;
				pop(A);
				//search(A);
			}
		}
	} 
} 



posted @ 2018-01-11 21:12  溡沭  阅读(121)  评论(0编辑  收藏  举报