当前位置: 代码迷 >> 综合 >> HDOJ 1873 看病要排队 (优先队列+贪心)
  详细解决方案

HDOJ 1873 看病要排队 (优先队列+贪心)

热度:55   发布时间:2024-01-15 05:07:43.0

http://acm.hdu.edu.cn/showproblem.php?pid=1873
中文题就不写题意了。

就三个医生,所以可以直接暴力开三个优先队列,第一关键字是优先级从大到小,第二关键字是来看病的时候从小到大,这里重载小于号要注意,因为STL里的priority_queue默认是大根堆,重载小于号时,返回小于其实堆顶是最大值。
然后就模拟这个过程即可,IN的时候push(),OUT的时候输出top()并pop()。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <stack>
#include <cstring>
#include <queue>using namespace std;
int n;
struct node{int b,t;};
bool operator < (const node& x,const node& y){if(x.b==y.b) return x.t>y.t;return x.b<y.b;
}
priority_queue <node> q1,q2,q3;int main()
{while(scanf("%d",&n)!=EOF){int num=0,x,y;char s[10];q1 = priority_queue<node>();q2 = priority_queue<node>();q3 = priority_queue<node>();for(int i=1;i<=n;i++){scanf("%s",s);if(s[0]=='I'){num++;scanf("%d%d",&x,&y);node tmp;tmp.t=num;tmp.b=y;switch(x){case 1:q1.push(tmp);break;case 2:q2.push(tmp);break;case 3:q3.push(tmp);break;}}if(s[0]=='O'){scanf("%d",&x);switch(x){case 1:{if(q1.empty())printf("EMPTY\n");else{printf("%d\n",q1.top().t);q1.pop();}break;}case 2:{if(q2.empty())printf("EMPTY\n");else{printf("%d\n",q2.top().t);q2.pop();}break;}case 3:{if(q3.empty())printf("EMPTY\n");else{printf("%d\n",q3.top().t);q3.pop();}break;}}}}}return 0;
}