#C11161. Priority Queue Visitor Distribution
Priority Queue Visitor Distribution
Priority Queue Visitor Distribution
You are tasked with simulating a visitor management system using a priority queue. In this system, visitors are added with a priority level and a unique identifier. The system will process three types of queries:
- 1 p id: Add a visitor with priority p and visitor ID id.
- 2: Remove the visitor with the highest priority from the queue. In the case of equal priorities, the visitor added earlier will be removed.
- 3 bookID: Distribute a book with ID bookID to the visitor with the highest priority currently in the queue (without removing the visitor). The system then outputs the visitor's ID and the book ID.
Note that if there is no visitor in the queue when a type "3" query is encountered, nothing should be printed for that query.
Tie Breaking: If two visitors have the same priority, the one who entered the queue earlier (i.e. with a lower insertion order) has the higher precedence.
The operations must be implemented efficiently using a priority queue (max-heap simulated via inversion or custom comparator) with a tie-breaker that respects the order of arrival.
inputFormat
The first line of input contains an integer Q, representing the number of queries. Each of the next Q lines contains a query in one of the following formats:
1 p id
: Add a visitor with an integer priority p and visitor ID id.2
: Remove the visitor with the highest priority from the queue.3 bookID
: Distribute a book with the given book ID to the visitor with the highest priority. (Do not remove the visitor.)
For queries of type 3
, output a line containing the visitor's ID and the book ID, separated by a space.
outputFormat
For each query of type 3
, output one line with two integers separated by a space: the visitor ID and the book ID. If a type 3
query is encountered when the queue is empty, no output should be produced for that query.## sample
2
1 5 101
2