HDU

来源:互联网 发布:我是拳王阿里 百度云 编辑:程序博客网 时间:2024/06/06 22:23

点我看题

题意:看病排队,首先根据病情的轻重缓急来医治,严重的先治,病情相同的情况下,先来的先治,根据输入输出病人的id.

分析:就是一个优先队列进队出队的问题,关键在于优先队列的重载小于号.

参考代码:

#include<cstdio>#include<cmath>#include<cstring>#include<algorithm>#include<queue>#include<iostream>using namespace std;int n;struct Node{int id;int val;friend bool operator < ( const Node &a, const Node &b){if( a.val != b.val)//从小到大return a.val < b.val;return a.id > b.id;//从大到小}};priority_queue<Node> q1;priority_queue<Node> q2;priority_queue<Node> q3;int main(){while( ~scanf("%d",&n)){while( !q1.empty())q1.pop();while( !q2.empty())q2.pop();while( !q3.empty())q3.pop();int cnt = 0;while( n--){char str[10];int a,b;scanf("%s",str);if( !strcmp("IN",str)){cnt++;scanf("%d%d",&a,&b);Node tmp;tmp.id = cnt;tmp.val = b;if( a == 1)q1.push(tmp);else if( a == 2)q2.push(tmp);elseq3.push(tmp);}else if( !strcmp("OUT",str)){Node tmp;scanf("%d",&a);if( a == 1 && !q1.empty()){tmp = q1.top();printf("%d\n",tmp.id);q1.pop();}else if( a == 2 && !q2.empty()){tmp = q2.top();printf("%d\n",tmp.id);q2.pop();}else if( a == 3 && !q3.empty()){tmp = q3.top();printf("%d\n",tmp.id);q3.pop();}elseputs("EMPTY");}}}return 0;}