This is a Chinese question. You can go to OJ to view the question directly.
Solution: it is not difficult to understand the question. In our real life, if we were looking for a doctor, would we have to queue up in his queue ???
This queue can be sorted based on multiple situations. At this time, we can consider using the following priority queue ....
The Code is as follows:
Version 1 (about ms ):
/** 1873_2.cpp ** created on: July 15, August 8, 2013 * Author: Administrator */# include <iostream> # include <queue> using namespace STD; struct node {int PRI; int num; public: Friend bool operator <(const node & A, const node & B) {if (. PRI! = B. PRI) {return. PRI <B. PRI;} return. num> B. num ;}; int main () {int N; while (CIN >>n) {priority_queue <node> q [4]; int B, Count = 1; string STR; node A; For (INT I = 0; I <n; ++ I) {CIN> STR; If (STR = "in ") {CIN> B>. PRI;. num = count ++; Q [B]. push (a) ;}else {CIN> B; If (! Q [B]. empty () {A = Q [B]. top (); Q [B]. pop (); cout <. num <Endl ;}else {cout <"empty" <Endl ;}}}}}
Version 2:
/** 1873_1.cpp ** created on: August 8, 2013 * Author: Administrator */# include "stdio. H "# include" string. H "# include" stdlib. H "# include" queue "using namespace STD; int tot; struct node {int PRI; int num; friend bool operator <(node N1, node N2) {If (n1.pri = n2.pri) return n2.num <n1.num; elsereturn n1.pri <n2.pri ;}}; int main () {int N; node now; int A, B; char STR [20]; while (scanf ("% d", & N )! =-1) {priority_queue <node> q [4]; Tot = 0; while (n --) {scanf ("% s", STR); If (strcmp (STR, "Out") = 0) {scanf ("% d", & A); If (Q [A]. empty () printf ("Empty \ n"); else {now = Q [A]. top (); Q [A]. pop (); printf ("% d \ n", now. num) ;}} else if (strcmp (STR, "in") = 0) {scanf ("% d", & A, & B); now. num = ++ tot; now. PRI = B; Q [A]. push (now) ;}} return 0 ;}