#P11773. Card Sorting by k-th Removal
Card Sorting by k-th Removal
Card Sorting by k-th Removal
Longya Ge has been given n cards by Alin and they are laid out in a row from left to right. The number on the i-th card is a_i. Longya’s goal is to arrange the cards so that the numbers are non‐decreasing from left to right. He is allowed to perform the following operation any number of times:
In one move, remove the card in the k-th position (from the left, where k is a given constant) and then insert that card either at the beginning (left end) or at the end (right end) of the row.
Please help Longya determine whether it is possible to achieve a non‐decreasing order. If it is, provide a valid (and simple) sequence of operations to do so.
Note: If the cards are already in non-decreasing order, no operations are needed.
The operations can be described by a string of characters, where 'L' denotes moving the k-th card to the left end, and 'R' denotes moving it to the right end.
The required answer is to either output NO if no sequence exists, or YES followed by a valid sequence.
The operations are performed on the current arrangement — after each move, the positions of the remaining cards are shifted accordingly, and k always refers to the k-th card in the current order.
inputFormat
The first line contains two integers n and k (1 ≤ k ≤ n ≤ 105), representing the number of cards and the fixed position, respectively.
The second line contains n integers a1, a2, …, an (−109 ≤ ai ≤ 109), representing the numbers on the cards in order from left to right.
outputFormat
If it is possible to arrange the cards in non-decreasing order using the allowed operation, first output a line containing the number of moves m (m ≥ 0). On the next line, output a string of length m formed by characters 'L' and 'R' representing one valid sequence of operations. (If no operation is needed, output 0 moves and an empty line.)
If it is impossible, output a single line containing the word NO.
sample
3 2
3 1 2
2
LR
</p>