A. Priority Queue
Priority Queue
You cannot submit for this problem because the contest is ended. You can click "Open in Problem Set" to view this problem in normal mode.
Description
In this assignment, you will implement a Priority Queue that supports three fundamental operations: pushing a key, peeking at the highest priority key, and popping the highest priority key.
A Priority Queue is an abstract data structure that maintains a collection of elements, each with an associated priority (key). For this problem, you will implement a Priority Queue, and treats larger keys as having higher priority.
A common and efficient way to implement this is by using a Max Heap. A Max Heap is a binary tree that satisfies two special properties:
- Shape Property: It is a complete binary tree. This means all levels of the tree are fully filled, except possibly the last level, which is filled from left to right.
- Max-Heap Property: The key of any node is greater than or equal to the keys of its children. This property ensures that the element with the maximum key is always at the root of the tree.
You will process a sequence of commands, each representing one of the following operations:
push k: Insert an integer keykinto the priority queue.peek: Get the maximum key from the priority queue without removing it. If the priority queue is empty, returnnull.pop: Extract the maximum key from the priority queue. This operation should remove and return the key. If the priority queue is empty, returnnull.
Format
Input
- The first line contains a single integer (), representing the number of operations to perform.
- The next lines each contain an operation. The operation will be one of the following:
push k: The stringpushfollowed by a single space and an integer key ().peek: The literal stringpeek.pop: The literal stringpop.
Output
- For each
peekorpopoperation, print the integer value of the key on a new line. If the priority queue is empty when the operation is called, printnull. - The
pushoperation should not produce any output.
Samples
7
push 10
push 5
push 15
peek
pop
peek
pop
15
15
10
10
Explanation: We can visualize the heap as an array.
push 10: Heap state:[10]push 5: Heap state:[10, 5]push 15: Heap state:[10, 5, 15]. Thesift-upoperation is performed on15. Heap becomes:[15, 5, 10].peek: The root15is the maximum. It is printed. The heap is unchanged. State:[15, 5, 10].pop: The root15is the maximum. It is printed and removed. The last element10moves to the root. Heap state:[10, 5]. Thesift-downoperation runs on10, but no swaps are needed.peek: The root10is the maximum. It is printed. Heap state:[10, 5].pop: The root10is the maximum. It is printed and removed. The last element5moves to the root. Heap state:[5].
13
push 50
push 30
push 70
peek
pop
push 20
push 40
peek
pop
pop
pop
pop
pop
70
70
50
50
40
30
20
null
Explanation:
- After
push 50,push 30,push 70, the heap state is[70, 30, 50]. peek: Prints70. Heap is unchanged.pop: Prints70. Heap becomes[50, 30].push 20: Heap becomes[50, 30, 20].push 40: Heap becomes[50, 30, 20, 40].sift-up(40)swaps it with30. Heap state:[50, 40, 20, 30].peek: Prints50. Heap is unchanged.pop: Prints50. Heap becomes[40, 30, 20].pop: Prints40. Heap becomes[30, 20].pop: Prints30. Heap becomes[20].pop: Prints20. Heap becomes[](empty).pop: The heap is empty. The output isnull.
Hint
- A solution with overall complexity (e.g., using an unsorted or sorted array) will be too slow.
- With a Max Heap implementation, insertion (
push) and extraction (pop) operations should ideally take about time, where is the current number of elements in the heap. Thepeekoperation should take time. - The test cases are structured as follows:
- Test Cases 1-5: Small-scale inputs to check the basic heap logic (sift-up, sift-down).
- Test Cases 6-15: Large-scale inputs with a mix of
push,pop, andpeekoperations to test efficiency and correctness, including calls on an empty heap.
Limitation
1s, 128MiB for each test case.
Assignment 2
- Status
- Done
- Problem
- 1
- Open Since
- 2025-10-20 9:30
- Deadline
- 2025-11-2 23:59
- Extension
- 72 hour(s)