Uvalive 6093 emergency room -- Simulation of priority queue implementation

Source: Internet
Author: User

Question: give n doctors a working time, and then give some patients, patients a time of arrival, and some diagnoses with property (priority) and duration (diagnostic time) attributes, each patient may have to be diagnosed multiple times, and finally asked how much time each patient had to leave the hospital after completing all the courses.

Analysis: Use the priority queue to store diagnostics, patients, and then simulate a diagnostic process. The process ends when the number of patients equals the number of patients. For more information, see the code.

Code:

# Include <iostream> # include <cstdio> # include <cstring> # include <cmath> # include <algorithm> # include <vector> # include <queue> # include <cstdlib> using namespace STD; # define n 100102 # define M 22 struct treat {int Tim, IND; treat (INT _ Tim, int _ IND): Tim (_ Tim), Ind (_ IND) {} treat () {} bool operator <(const treat & A) const {return Tim>. tim ;}}; struct node {int X, Y; node (INT _ x, int _ y): X (_ x), y (_ y) {} node () {}}; Struct patient {int ID, Pro, arrtim; patient (INT _ id, int _ Pro, int _ arrtim): ID (_ id), Pro (_ Pro ), arrtim (_ arrtim) {} patient () {} bool operator <(const patient & A) const {if (. pro = Pro) return arrtim>. arrtim; return pro <. pro ;}}; priority_queue <treat> T; priority_queue <patient> P; vector <node> Pat [506], ans; int arrive [506], treated [506]; int CMP (node ka, node KB) {If (Ka. X = kb. x) return ka. Y <kb. y; retu Rn ka. x <kb. x;} int main () {int N, Tim, A, B, PCNT, cs = 1; int I, j; while (scanf ("% d ", & N, & Tim )! = EOF & (n + Tim) {PCNT = 0; ans. clear (); While (scanf ("% d", & arrive [PCNT]) & arrive [PCNT]! =-1) {Pat [PCNT]. clear (); While (scanf ("% d", & A, & B) & (a + B) // property, duration Pat [PCNT]. push_back (node (A, B); PCNT ++;} while (! T. Empty () T. Pop (); While (! P. empty () p. pop (); for (I = 0; I <n; I ++) // n doctors prepare T. push (treat (TIM,-1); for (I = 0; I <PCNT; I ++) T. push (treat (arrive [I], I); // No. I int doctor = 0; memset (treated,-1, sizeof (treated); int PCNT = 0; while (PCNT <PCNT) {int nowt = T. top (). tim; while (! T. empty () & T. top (). tim = nowt) {If (T. top (). IND =-1) // idle doctor ++; else {int pid = T. top (). IND; treated [pid] ++; If (treated [pid]> = Pat [pid]. size () // All courses are completed {ans. push_back (node (nowt, arrive [pid]); PCNT ++;} else p. push (patient (PID, Pat [pid] [treated [pid]. x, arrive [pid]);} t. pop () ;}while (! P. empty () & doctor) {int K = P. top (). ID; T. push (treat (nowt + Pat [k] [treated [k]. y,-1); // T. push (treat (nowt + Pat [k] [treated [k]. y, k); // or continue to diagnose this patient p. pop (); Doctor --;} Sort (ans. begin (), ans. end (), CMP); printf ("case % d: \ n", CS ++); for (I = 0; I <ans. size (); I ++) printf ("patient % d released at clock = % d \ n", ANS [I]. y, ANS [I]. x);} return 0 ;}
View code

 

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.