题目1873: 看病要排队
7IN 1 1IN 1 2OUT 1OUT 2IN 2 1OUT 2OUT 12IN 1 1OUT 1
2EMPTY311
/********************************* * 日期:2013-3-16 * 作者:SJF0115 * 题号: HDU 题目1873: 看病要排队 * 来源:http://acm.hdu.edu.cn/showproblem.php?pid=1873 * 结果:AC * 来源:2008浙大研究生复试热身赛(2)——全真模拟 * 总结: **********************************/#include<iostream>#include<stdio.h>#include<queue>using namespace std;struct Patient { //值 int priority;//编号 int key; //重载操作符 friend bool operator < (Patient p1,Patient p2) { if(p1.priority != p2.priority){return p1.priority < p2.priority;}else{return p1.key > p2.key;} }};int main(){int i,N,k;char Type[4];int DoctorID,PatientID;Patient patient[2001];while(scanf("%d",&N) != EOF){//定义三个医生priority_queue<Patient> Doctor1;priority_queue<Patient> Doctor2;priority_queue<Patient> Doctor3;k = 1;while(N--){scanf("%s",Type);//诊治if(strcmp(Type,"IN") == 0){//输入病人和医生patient[k].key = k;scanf("%d %d",&DoctorID,&patient[k].priority);//排队if(DoctorID == 1){Doctor1.push(patient[k]);}else if(DoctorID == 2){Doctor2.push(patient[k]);}else{Doctor3.push(patient[k]);}k++;}//出院else if(strcmp(Type,"OUT") == 0){//医生DoctorID进行了一次诊治,诊治完毕后,病人出院scanf("%d",&DoctorID);//医生1if(DoctorID == 1){if(Doctor1.empty()){printf("EMPTY\n");}else{printf("%d\n",Doctor1.top().key);//出院Doctor1.pop();}}//医生2else if(DoctorID == 2){if(Doctor2.empty()){printf("EMPTY\n");}else{printf("%d\n",Doctor2.top().key);//出院Doctor2.pop();}}//医生3else{if(Doctor3.empty()){printf("EMPTY\n");}else{printf("%d\n",Doctor3.top().key);//出院Doctor3.pop();}}}}}return 0;}注意:此题用的是优先级队列
如果不了解优先级队列,请参考博文:点击打开链接