HDU 1873 (waiting for medical treatment)

Source: Internet
Author: User
Tags strcmp

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 ;}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.