看病要排队
这个题是典型的排队的问题,用优先队列可以解决。主要学习优先队列和优先队列结构体类型的函数重载。
具体看代码:
#include<iostream>
#include<cstdio>
#include<queue>
using namespace std;
struct sean
{
int num;
int pri;
}; //定义结构体类型
bool operator < (const sean &x,const sean &y)//函数体类型的函数重载,应用于优先队列,注意写法
{
if(x.pri!=y.pri)//不等于时
{
return x.pri<y.pri;//注意,优先队列的本质是大顶堆或小顶堆,所以>号相当于小于
}
else
{
return x.num>y.num;//此处也一样
}
}
int main()
{
sean book;//定义结构体类型的数据结构
int n,num1;
int A,B;
string s;
while(scanf("%d",&n)!=EOF)
{
priority_queue<sean> q[4];//优先队列可以像数组一样开好几个
num1=0;
while(n--)
{
getchar();//注意空格的处理
cin>>s;
if(s=="IN")
{
num1++;
cin>>A>>B;
book.num=num1;
book.pri=B;
q[A].push(book);//结构体类型的数据集合入队是是以一个整体放入的
}
else
{
cin>>A;
if(q[A].empty()==true)//判断队列是否为空
{
printf("EMPTY\n");
}
else
{
book=q[A].top();//取队首元素
cout<<book.num<<endl;
q[A].pop();//出队首元素
}
}
}
}
return 0;
}