G. 2023夏-A-2 Queue Using Two Stacks

    传统题 400ms 64MiB

2023夏-A-2 Queue Using Two Stacks

该比赛已结束,您无法在比赛模式下递交该题目。您可以点击“在题库中打开”以普通模式查看和递交本题。

题目描述

A queue (FIFO structure) can be implemented by two stacks (LIFO structure) in the following way:

  1. Start from two empty stacks s1s_1 and s2s_2.
  2. When element ee is enqueued, it is actually pushed onto s1s_1.
  3. When we are supposed to dequeue, s2s_2 is checked first. If s2s_2 is empty, everything in s1s_1 will be transferred to s2s_2 by popping from s1s_1 and immediately pushing onto s2s_2. Then we just pop from s2s_2 -- the top element of s2s_2 must be the first one to enter s1s_1 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 NN (103\le 10^3), which are the number of operations. Then NN 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 10610^6. 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

PAT2023夏季重现赛

未参加
状态
已结束
规则
IOI
题目
9
开始于
2025-11-23 18:30
结束于
2025-11-23 22:00
持续时间
3.5 小时
主持人
参赛人数
31