#C12073. Processing Queue Events
Processing Queue Events
Processing Queue Events
You are given a sequence of events that simulate people entering and leaving a queue. Each event is represented by an integer. A positive integer indicates that a person with that ID enters the queue, and a negative integer indicates that the person with the absolute value of that integer leaves the queue. If a person who is not in the queue receives a leave event, the event is ignored. Similarly, if a person who is already in the queue receives an enter event, the event is ignored.
Your task is to determine the final state of the queue after processing all events in each test case. The final state should display the IDs in the order they entered and have not yet left, separated by a single space. If the queue is empty, print EMPTY
.
Note:
- Each test case begins with an integer M (the number of events).
- The following M integers denote the events.
- All IDs are positive integers.
Example:
Let \(M = 5\) and events = [1, -1, 2, -2, 1]. The process is as follows:
- Event 1: Person 1 enters. Queue: [1]
- Event -1: Person 1 leaves. Queue: []
- Event 2: Person 2 enters. Queue: [2]
- Event -2: Person 2 leaves. Queue: []
- Event 1: Person 1 enters again. Queue: [1]
The final queue is [1], so the output is 1
.
inputFormat
The input is read from standard input (stdin) and has the following format:
T M E1 E2 ... EM ... (repeated for T test cases)
Where:
- T is the number of test cases.
- For each test case, M is the number of events.
- Ei are the events (integers) for that test case.
outputFormat
For each test case, output a single line on standard output (stdout) representing the final state of the queue. Print the remaining IDs in the queue in their original order, separated by a single space. If the queue is empty, print EMPTY
.
3
5
1 -1 2 -2 1
4
4 3 -3 -4
3
5 5 -5
1
EMPTY
EMPTY
</p>