#K50242. Simulate Stack Operations
Simulate Stack Operations
Simulate Stack Operations
This problem requires you to simulate operations on a stack. The stack supports three types of operations:
push X
: Push the integer X onto the stack.pop
: Remove the element at the top of the stack. If the stack is empty, no element is removed.inc X Y
: Increment each of the bottom X elements of the stack by Y. In mathematical terms, if the stack is represented as an array \(S[0\ldots{k-1}]\) where \(S[0]\) is the bottom element and \(S[k-1]\) is the top, then the operation does: \(S[i] = S[i] + Y\) for all \(0 \le i < \min(X, k)\).
After performing each operation, output the current top element of the stack. If the stack is empty, output EMPTY
.
inputFormat
The input is given via stdin and has the following format:
- The first line contains an integer \(n\), the number of operations.
- The next \(n\) lines each contain one operation in one of the following formats:
push X
: where X is an integer.pop
inc X Y
: where X and Y are integers.
Note: If a pop
operation is performed when the stack is empty, it is ignored.
outputFormat
Output the result of each operation via stdout. Each result should be printed on a new line. For every operation, if the stack is empty, output EMPTY
; otherwise, output the integer at the top of the stack.
2
push 4
push 3
4
3
</p>