#C5233. Stack Operations with Maximum Query
Stack Operations with Maximum Query
Stack Operations with Maximum Query
You are given a series of operations to be performed on a stack. The operations include:
push x
: Push the integerx
onto the stack.pop
: Remove the top element from the stack if it exists.max
: Query the maximum element in the stack. If the stack is empty, returnNone
.
Your task is to simulate these operations. For each max
operation, output the current maximum element in the stack. Note that the underlying algorithm should support efficient maximum querying.
In this problem, you are required to implement the logic using the standard input (stdin) and standard output (stdout) methods.
The commands must be processed in the order given.
inputFormat
The first line contains an integer n, denoting the number of operations. The following n lines each contain one of the following commands:
push x
wherex
is an integer.pop
max
Input is given from stdin.
outputFormat
For each max
command encountered in the input, output the maximum element in the stack at that time. If the stack is empty when a max
command is executed, output None
. Each result should be printed on a new line to stdout.
5
push 3
push 2
max
pop
max
3
3
</p>