题目传送门:hdu1873看病要排队
这道题可以使用优先队列,然后自定义优先级。其余的注意细节然后就没什么问题了。
AC代码如下:
#include<queue>
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
using namespace std;
struct man
{
int du,num;
friend bool operator<(const man &a,const man &b)
{
if(a.du==b.du)
return a.num>b.num;
else
return a.du<b.du;
}
};
//设置三个优先队列。
priority_queue<man>D1;
priority_queue<man>D2;
priority_queue<man>D3;
int number,doctor,bing;
string ord;
void init()
{
while(!D1.empty())
D1.pop();
while(!D2.empty())
D2.pop();
while(!D3.empty())
D3.pop();
}
void showtime()
{
while(!D1.empty())
{
cout<<"病情级别:"<<D1.top().du<<"病人编号:"<<D1.top().num<<endl;
D1.pop();
}
while(!D2.empty())
{
cout<<"病情级别:"<<D2.top().du<<"病人编号:"<<D2.top().num<<endl;
D2.pop();
}
while(!D3.empty())
{
cout<<"病情级别:"<<D3.top().du<<"病人编号:"<<D3.top().num<<endl;
D3.pop();
}
}
int main()
{
while(cin>>number)
{
init();
int cnt=0;
for(int i=1; i<=number; i++)
{
cin>>ord;
if(ord=="IN")
{
cnt++;
cin>>doctor>>bing;
man q;
q.du=bing,q.num=cnt;
switch(doctor)
{
case 1:
D1.push(q);
break;
case 2:
D2.push(q);
break;
case 3:
D3.push(q);
break;
}
}
//showtime();
if(ord=="OUT")
{
cin>>doctor;
switch(doctor)
{
case 1:
if(!D1.empty())
{
man Q=D1.top();
D1.pop();
cout<<Q.num<<endl;
}
else
cout<<"EMPTY"<<endl;
break;
case 2:
if(!D2.empty())
{
man Q=D2.top();
D2.pop();
cout<<Q.num<<endl;
}
else
cout<<"EMPTY"<<endl;
break;
case 3:
if(!D3.empty())
{
man Q=D3.top();
D3.pop();
cout<<Q.num<<endl;
}
else
cout<<"EMPTY"<<endl;
break;
}
}
}
}
return 0;
}
原文地址:http://blog.csdn.net/u014004096/article/details/44757067