With a single-queue simulation, there is an array of CNT records of the number of 9 priority tasks, each time finding the current maximum priority task then out of the queue and updating the CNT array in a timely manner.
1#include <iostream>2#include <cstring>3#include <algorithm>4#include <cstdio>5#include <queue>6 using namespacestd;7 8 structTask9 {Ten intpos, pri; OneTask (intpos =0,intPRI =0):p OS (POS), PRI (PRI) {} A }; - - intcnt[Ten]; the - intMain () - { - //freopen ("In.txt", "R", stdin); + - intT scanf"%d", &T); + while(t--) A { at intn, p; -scanf"%d%d", &n, &p); -memset (CNT,0,sizeof(CNT)); -Queue<task>Q; - for(inti =0; I < n; i++) - { in intpri; -scanf"%d", &pri); to Q.push (Task (i, PRI)); +cnt[pri]++; - } the while(1) * { $Task T =Q.front ();Panax Notoginseng intm; - for(M =9; M >= T.pri; m--)if(Cnt[m]) Break; thecnt[m]--; + if(M >T.pri) A { the while(T.pri! =m) + { - Q.pop (); $ Q.push (t); $t =Q.front (); - } - Q.pop (); the if(T.pos = = p) Break; - //printf ("Sz =%d\n", q.size ());Wuyi } the Else - { Wu Q.pop (); - if(T.pos = = p) Break; About } $ } -printf"%d\n"Nq.size ()); - } - A return 0; +}code June
UVa 12100 (analog) Printer Queue