Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5838 Accepted Submission(s): 2405
7 IN 1 1 IN 1 2 OUT 1 OUT 2 IN 2 1 OUT 2 OUT 1 2 IN 1 1 OUT 1
2 EMPTY 3 1 1
解题思路:将每个病人的信息储存为一个结构体,将每个医生的求诊信息当成队列数组中的元素。以病情轻重,若病情相同,则以病人来到医院的先后作为队列的优先级。
具体代码如下:
#include<cstdio> #include<queue> #include<cstring> using namespace std; struct per { int num,id; }s; //先定义结构体,再写重载函数定义优先级比较简单明了 bool operator < (const per &x,const per &y)//重载 "<" 操作符定义优先级 { if(x.num==y.num) return x.id>y.id; else return x.num<y.num; } int main() { int n,a,b; char str[5]; while(scanf("%d",&n)!=EOF) { int k=1; priority_queue<per>q[4]; while(n--) { scanf("%s%d",str,&a); if(strcmp(str,"IN")==0) { scanf("%d",&b); s.num=b; s.id=k++; q[a].push(s); } else { if(!q[a].empty()) { s=q[a].top(); q[a].pop(); printf("%d\n",s.id); } else printf("EMPTY\n"); } } } return 0; }