Queue Sorting
Queue - a data structure that operates on the "first in, first out" (FIFO) principle. In a queue, you can only add elements at the end and remove them from the beginning. This means elements are removed in the same order they were added.
The task of sorting involves arranging a given array of numbers (or other objects) in either ascending or descending order. There are many variations of this task, with several efficient algorithms available.
In this problem, we consider a special device that includes an input stream, an output stream, and k queues, numbered from 1 to k.
Your task is to write a program for this device. Given a sequence of distinct numbers that need to be sorted, the program should output these numbers in ascending order, as they were received in the input stream.
The program must perform exactly 2n operations. Each operation either reads a number from the input stream and adds it to one of the queues, or removes a number from one of the queues and outputs it to the output stream.
Input
The first line contains an integer n (1 ≤ n ≤ 300). The second line contains n distinct integers a[1]
, ..., a[n]
in the order they arrive from the input stream (1 ≤ a[i]
≤ 10^9
for all i from 1 to n). The third line contains an integer k (1 ≤ k ≤ n).
Output
If sorting is impossible, output the word NO.
Otherwise, output the word YES followed by 2n lines that define the sorting program. Each line should describe one operation in the following format:
I(j) - read an element from the input stream and add it to queue number j (1 ≤ j ≤ k);
R(j) - remove an element from queue number j (1 ≤ j ≤ k) and output it to the output stream.