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