HDU ACM 1873 就诊要排队->优先队列
HDU ACM 1873 看病要排队->优先队列
分析:该題可以使用STL自带的优先队列实现,非常方便简单。注意在重载小于运算符的时候要写成友元函数,否则会编译不过。
#include<iostream> #include<queue> using namespace std; struct Node { int id,weight; Node(int _id,int _weight):id(_id),weight(_weight) { } friend bool operator<(Node a,Node b) { if(a.weight!=b.weight) return a.weight<b.weight; else return a.id>b.id; //如果优先级相同,则最早来排队的病人先诊断 } }; int main() { int n,i,A,B,k; char str[10]; while(cin>>n) { priority_queue<Node> q[4]; k=0; for(i=0;i<n;i++) { cin>>str; if(str[0]=='I') { cin>>A>>B; q[A].push(Node(++k,B)); } else if(str[0]=='O') { cin>>A; if(q[A].empty()) cout<<"EMPTY"<<endl; else { cout<<q[A].top().id<<endl; q[A].pop(); } } } } return 0; }