#C13824. Implement a Multi-Level Priority Queue
Implement a Multi-Level Priority Queue
Implement a Multi-Level Priority Queue
In this problem, you are required to implement a multi-level priority queue. The queue is divided into several priority levels where a lower numeric value indicates a higher priority (i.e. level 0 is the highest). The data structure must support two operations:
- enqueue: Add an element along with its priority.
- dequeue: Remove and return the front element from the highest priority (non-empty) level. If all levels are empty, return -1.
More formally, if we denote the number of priority levels as \(L\) and the operation to add an element \(x\) with priority \(p\) by enqueue(x, p)
, then the \(dequeue()\) function should return the element that was inserted earliest among all elements with the smallest priority value. If there is no element available, output \(-1\). The operations are to be processed sequentially from the input.
inputFormat
The input is given via standard input as follows:
The first line contains two integers (n) and (L), where (n) is the number of operations and (L) is the number of priority levels (with valid priorities from (0) to (L-1)).
Each of the next (n) lines contains an operation in one of the following forms:
- "E x p" : Enqueue an element (x) with priority (p).
- "D" : Dequeue the front element from the highest non-empty priority level and output it. If all queues are empty, output (-1).
outputFormat
For each dequeue operation, output the result on a separate line to standard output.## sample
9 3
E 5 2
E 3 0
E 7 1
E 1 0
D
D
D
D
D
3
1
7
5
-1
</p>