CMG - Collecting Mango

One day after storm mina went to pick up mangoes in the garden with a basket. She began to pick up mangoes from the garden. And if she wants, she can throw away the last picked up mango from the basket. In this way, mina kept picking up mangoes. She brought you with her to keep track of the biggest size of mango in the basket at that time. At any moment Mina can ask you about the biggest size of mango. Your job is to help Mina.

Since you are a good programmer, so you write a program by which you are easily able to answer the question of Mina.

During picking up mangoes, Mina can have 3 types of question/instruction for you.

  • Type 1: put an ‘x’ size mango in the basket, which is picked up form garden.
  • Type 2: Throw out last picked up mango. (There can be consecutive throw out operation.)
  • Type 3: Ask for the biggest mango size in the basket at that moment.

Input

The first line contains a positive integer T, number of test case. In the following each case start with a positive integer N, which is number of question/operation Mina will ask during picking up mangoes. Next N lines will contain 3 types of operations (A, R, Q). A x, here x is picked up mango size. R, throw out last picked up mango from basket. Q Find out the biggest size mango.

Output

For each case, first print the case number and print the answer of Mina’s question. Whenever the basket is empty, then Mina’s question’s answer will be “Empty”.

Constraints

  • 1<=T<=25
  • 1<=N<=100000
  • 1<=x<=100000

Example

Input:
2
6
A 10
A 5
Q
A 100
R
Q
6
A 5
Q
R
Q
R
R

Output:
Case 1:
10
10
Case 2:
5
Empty

Added by:Anick
Date:2016-04-24
Time limit:0.5s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: GOSU
Resource:Own problem. Used in NHSPC 2016 Final Round. More about NHSPC: http://www.nhspc.org/

hide comments
2019-10-07 21:45:43
Stack with O(1) findMax() method and TLE :/
I had to use std::ios_base::sync_with_stdio(false);
2019-05-11 20:01:50
solved in pypy..using 2 deque and standard i/o ..worth try ...
2018-08-25 14:07:48 Nour Alhadi
Very unusual for me, O(n) algorithm with scanf() gives TLE, with ios_base AC :-\ :-\
2018-06-20 06:00:24
stacks rocked but cin cout shocked
2018-06-19 19:58:28
TLE with ios_base but AC with scanf/printf
2017-03-26 19:40:47
easy one with vectors..
2017-02-22 17:01:40
How many of you found algorithm for O(1) append, O(1) out and O(1) max?
Hint: Use 2 vectors... One for main array, another for Max so far ;)

Last edit: 2017-02-22 17:05:01
2017-01-26 15:50:44 Shivam Gupta
fast i/o not needed, even n*log^2 n passes in .40
2016-11-28 16:58:27 prakash
easy one using fast I/O
2016-08-27 11:28:37 lt
Linear time algorithm giving TLE :(
Fast IO => AC :)

Last edit: 2016-08-27 12:32:41
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.