#P9518. queue

queue

Background

You are right, but MaiMai DX is a game and then I forgot the rest.

Problem Description

Additional note: The “queueing” here is different from the usual one. The people who are currently playing are considered the first two positions of the queue, so playing is treated as queueing.

There is an arcade with one game machine, and at most two people can play at the same time. But clearly, more than two people may come to play! So they need to line up. You need to write a program to maintain this queue, and notify the next players after others finish playing. During the whole process, the following events may happen:

  • start: A round of the game starts. If this is not the first round, then the participants of the previous round return to the back of the queue in their original order at the instant right before this round starts. Now you should output the names of the people who will play this round in the order in the queue (normally the first two people in the queue, or the only one). If there are two, separate them with a space. If nobody plays in this round, output Error and ignore this event.

  • arrive x: xx arrives at the arcade and joins the back of the queue. At this time, xx should not already be in the queue; otherwise output Error and ignore this event. If the event is executed successfully, output OK.

  • leave x: xx leaves the arcade and leaves the queue. At this time, xx should be in the queue but should not be playing; otherwise output Error and ignore this event. If the event is executed successfully, output OK.

You need to maintain the queue information and output what is required for the events above.

Input Format

The first line contains an integer nn, the number of events.

The next nn lines each describe one event.

Output Format

Output nn lines as required by the statement, representing the program’s response to each event.

14
start
arrive A
start
arrive B
arrive C
arrive D
start
leave C
leave D
start
arrive A
arrive D
leave E
start
Error
OK
A
OK
OK
OK
B C
Error
OK
A B
Error
OK
Error
C D
3
arrive A
arrive B
arrive C
OK
OK
OK

Hint

Sample Explanation

In sample 11, the following events happen:

  • At the first start, the queue is empty, so output Error.
  • A then joins the queue.
  • At the second start, there is only A, so output A.
  • B, C, D then join the queue in order.
  • At the third start, B, C play.
  • C tries to leave, but he is playing, so output Error.
  • D leaves successfully.
  • At the fourth start, A, B play.
  • A tries to join the queue, but he is already in the queue, so output Error.
  • D joins the queue again.
  • E tries to leave, but is not in the queue at all, so output Error.
  • At the fifth start, C, D play.

In sample 22, A, B, C join the queue in order. All operations are legal, so output three OK.

Constraints

For 20%20\% of the testdata, it is guaranteed that n=1n=1.

For 40%40\% of the testdata, it is guaranteed that n2000n \le 2000.

For another 20%20\% of the testdata, it is guaranteed that there is no leave operation.

For another 20%20\% of the testdata, names can only be a single uppercase letter.

For 100%100\% of the testdata, it is guaranteed that 1n1051 \le n \le 10^5, and each name contains only uppercase and lowercase letters and has length at most 1010.

The input and output size of this problem is large, so please use efficient I/O methods.

Translated by ChatGPT 5