#C13172. Efficient Min-Tracking List
Efficient Min-Tracking List
Efficient Min-Tracking List
This problem requires you to implement a custom list data structure that supports the following operations efficiently:
push x
: Append the integerx
to the end of the list.pop
: Remove the last element from the list.top
: Retrieve the last element in the list without removing it.get_min
: Retrieve the minimum element in the list in constant time.
Your task is to process a series of commands. For each command that produces an output (top
and get_min
), you should print the result on a new line. If an operation is invalid (for example, calling top
or get_min
on an empty list, or pop
on an empty list), output error
.
Note that the get_min
operation must run in constant time, i.e. in \(O(1)\) time.
inputFormat
The first line of input contains a single integer \(n\) representing the number of commands. The following \(n\) lines each contain a command. The commands can be:
push x
wherex
is an integer.pop
top
get_min
outputFormat
For each command that produces an output (top
or get_min
), print the result on its own line. If a command cannot be executed because the list is empty, output error
.
6
push 5
push 3
top
get_min
pop
get_min
3
3
5
</p>