#P2776. [SDOI2007] 小组队列
[SDOI2007] 小组队列
Description
There are groups and elements. Each element belongs to exactly one group.
Support the following operations:
push x: Enqueue element x. If there is already an element from x’s group ahead in the queue, x will be placed immediately after the last element of its own group currently in the queue; otherwise, x will be placed at the end of the whole queue.
pop: Dequeue. Remove the front element of the queue and output it. This behaves like a normal queue, i.e., elements in front leave first.
Input Format
The first line contains two positive integers , , representing the number of elements and the number of groups. Elements and groups are numbered starting from .
The next line contains non-negative integers , where is the group that element belongs to.
The next line contains a positive integer , the number of operations.
The next lines each contain one operation.
Output Format
For each dequeue operation, output the dequeued element on a separate line.
4 2
0 0 1 1
6
push 2
push 0
push 3
pop
pop
pop
2
3
0
Hint
For of the testdata, , , .
For of the testdata, , , . The input guarantees that all operations are valid.
Translated by ChatGPT 5
京公网安备 11011102002149号