此题要注意以下一句话:
Note that one message can appear twice or more and if two messages have the same priority, the one comes first will be processed first.(i.e., FIFO for the same priority.) Process to the end-of-file.
然后用一个优先队列就可以了。
# include <iostream> # include <stdio.h> # include <queue> # include <string.h> using namespace std; struct node{ char Name[100]; int Date; int Priority; int num; bool operator < (const node &a)const { if(a.Priority!=Priority) return a.Priority<Priority; else return a.num<num; } }; int main() { priority_queue<node> v; char x[100]; node y; int q=1; while(scanf("%s",x)!=EOF) { if(x[0]=='G') { if(!v.empty()) { printf("%s %d\n",v.top().Name,v.top().Date); v.pop(); } else printf("EMPTY QUEUE!\n"); } else { scanf("%s%d%d",y.Name,&y.Date,&y.Priority); y.num=q; q++; v.push(y); } } return 0; }