#C4530. Enhanced Stack with Undo and Maximum Query
Enhanced Stack with Undo and Maximum Query
Enhanced Stack with Undo and Maximum Query
This problem requires you to implement an enhanced stack with four operations: push
, pop
, max
, and undo
. The push
operation adds an element to the top of the stack, pop
removes the top element, and max
returns the maximum element in the stack. The maximum is defined as the highest element according to the relation \(a \ge b\). The undo
operation reverts the most recent push
or pop
operation, restoring the stack to its previous state.
You will be given a series of commands. When a max
command is encountered, output the current maximum element in the stack on a new line.
inputFormat
The first line contains an integer \(n\), representing the number of operations. The following \(n\) lines each contain one of the following commands:
push x
: Push the integer \(x\) onto the stack.pop
: Remove the element on top of the stack.max
: Print the maximum element in the stack.undo
: Revert the most recentpush
orpop
operation.
outputFormat
For each max
command in the input, output the current maximum element in the stack on a new line.
7
push 3
push 5
max
pop
max
undo
max
5
3
5
</p>