#P3516. Block Rearrangement Challenge
Block Rearrangement Challenge
Block Rearrangement Challenge
Byteasar bought his son Bytie a set of blocks numbered from \(1\) to \(n\) and arranged them in a row in a certain order. Bytie's goal is to rearrange the blocks so that they are in natural order, from the smallest number to the largest.
The only moves allowed are:
- Move \(a\): Take the last block and put it at the very beginning.
- Move \(b\): Take the third block and put it at the very beginning.
Your task is to determine whether a given arrangement can be sorted using these moves, and if so, provide the sequence of moves.
If the blocks are already sorted, output 0 moves. If no sequence of moves can sort the blocks, output NO
.
inputFormat
The input consists of two lines:
- The first line contains a positive integer \(n\) representing the number of blocks.
- The second line contains \(n\) space-separated integers representing the current order of the blocks.
outputFormat
If the blocks can be rearranged into natural order \(1, 2, \dots, n\) using the allowed moves, output the following:
- On the first line, output
YES
. - On the second line, output an integer \(m\) representing the number of moves.
- On the third line, output a string of length \(m\) consisting of characters
a
andb
which denote the sequence of moves performed (in order from first to last move).
If the blocks cannot be sorted using the allowed moves, output a single line containing NO
.
sample
3
1 2 3
YES
0
</p>