Minimum Heap
InterviewTime limit1sMemory limit128 MB
Implement a min-heap to support inserting natural numbers and repeatedly extracting the smallest, printing 0 when empty.
- Level
Easy3 of 10
- Topics
- Heap, Implementation
- Solved
- No attempts yet
Problem
Write a program that manages a collection of natural numbers with a min-heap. The collection starts empty and must support the following operations.
- If a natural number
xis given, insertxinto the collection. - If
0is given, print the smallest value in the collection and remove that value. If the collection is empty, print0.
Input
The first line contains the number of operations N (1 <= N <= 100,000). Each of the next N lines contains one integer x describing an operation.
If x is a natural number, insert x into the collection. If x is 0, print and remove the smallest value in the collection. Each x is either 0 or a natural number less than 2^31; negative integers are not given.
Output
For each operation where 0 is given, print one line containing the result. If the collection is empty when a print-and-remove operation is requested, print 0.