Uvalive 3135 Argus
http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=18684
Test Instructions
Write a system to execute a series of REGISTER commands: Register q_num Period, each command execution cycle is Period, execution event Q_num, if the event occurs simultaneously, the priority execution Q_num small.
Example
Sample Input
Register 2004 200
Register 2005 300
#
5
Sample Output
2004
2005
2004
2004
2005
Ideas★ Typical Multi-path merge problem, with priority queue to maintain the next event that will occur, priority queue content to advance
defining priority Levels, Total time complexity O (KLOGN)
Reference Code
#include <bits/stdc++.h>using namespace STD;Const int_max =2e4+Ten;Chars[ -];intNstructitem{intQ_num,period,time;BOOL operator< (ConstItem &a)Const{//const is essential to explain the low priority return(Time > A.time) | | (Time = = A.time && q_num > A.q_num); }};p riority_queue<item>pq;intMain () {#ifndef Online_judgeFreopen ("Input.txt","R", stdin);#endif //Online_judge Item item; while(scanf('%s ', s) = =1&& s[0] !=' # '){scanf("%d%d", &item. Q_num,&item. Period); Item.time = Item. Period; Pq.push (item); }scanf("%d", &n); for(inti =0; I < n; + + i) {item = Pq.top ();//Heap TopPq.pop ();printf("%d\n", item. Q_num); Item.time + = Item. Period;//Update the time of the next eventPq.push (item); }return 0;}
- Bold
Ctrl + B
- Italic Body
Ctrl + I
- Reference
Ctrl + Q
- Insert Link
Ctrl + L
- Inserting code
Ctrl + K
- Insert Picture
Ctrl + G
- Promote title
Ctrl + H
- Ordered list
Ctrl + O
- Unordered list
Ctrl + U
- Line
Ctrl + R
- Revoke
Ctrl + Z
- Redo
Ctrl + Y
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
"Priority queue Multi-merge" Uvalive 3135 Argus