2023夏-A-2 Queue Using Two Stacks
该比赛已结束,您无法在比赛模式下递交该题目。您可以点击“在题库中打开”以普通模式查看和递交本题。
题目描述
A queue (FIFO structure) can be implemented by two stacks (LIFO structure) in the following way:
- Start from two empty stacks and .
- When element is enqueued, it is actually pushed onto .
- When we are supposed to dequeue, is checked first. If is empty, everything in will be transferred to by popping from and immediately pushing onto . Then we just pop from -- the top element of must be the first one to enter thus is the first element that was enqueued.
Assume that each operation of push or pop takes 1 unit of time. You job is to tell the time taken for each dequeue.
输入格式
Each input file contains one test case. For each case, the first line gives a positive integer (), which are the number of operations. Then lines follow, each gives an operation in the format
Operation Element
where Operation being I represents enqueue and O represents dequeue. For each I, Element is a positive integer that is no more than . No Element is given for O operations.
It is guaranteed that there is at least one O operation.
输出格式
For each dequeue operation, print in a line the dequeued element and the unites of time taken to do this dequeue. The numbers in a line must be separated by 1 space, and there must be no extra space at the beginning or the end of the line.
In case that the queue is empty when dequeue is called, output in a line ERROR instead.
样例
10
I 20
I 32
O
I 11
O
O
O
I 100
I 66
O
20 5
32 1
11 3
ERROR
100 5
限制
对于所有的测试用例,限制为400ms, 64MB