2023冬-A-2 PeekMax in Stack
该比赛已结束,您无法在比赛模式下递交该题目。您可以点击“在题库中打开”以普通模式查看和递交本题。
题目描述
A stack is a First-In-Last-Out linear structure. Here you are supposed to implement a special function that allows you to find the maximum key value (PeekMax) in a stack, besides the normal Push and Pop. The point is, this new function must not slow down the overall performance of this stack.
输入格式
Each input file contains one test case. For each case, the first line gives a positive integer (), which is the number of operations. Then lines follow, each gives an operation in the format:
Push X
, means to push elementX
onto the stack, whereX
is an integer of which the absolute value is no more than ;Pop
means to pop the top element out of the stack;PeekMax
means to return the maximum value of the keys in the current stack.
输出格式
For each operation, execute as it is defined. For each Pop
, print in a lien the key value of the popped element; and for each PeekMax
, print in a line the maximum value of the keys in the current stack. If the stack is empty when Pop
or PeekMax
is called, output ERROR
instead.
It is guaranteed that there is at least one line of output.
样例
11
Pop
Push 34
Push 28
PeekMax
Push 84
PeekMax
Pop
PeekMax
Pop
Pop
PeekMax
ERROR
34
84
84
34
28
34
ERROR
限制
Java (javac) 时间限制 650 ms 内存限制 256 MB
Python (python3) 时间限制 250 ms 内存限制 64 MB
其他编译器 时间限制 150 ms 内存限制 64 MB