#P8223. Circular Shift Permutation Recovery

    ID: 21402 Type: Default 1000ms 256MiB

Circular Shift Permutation Recovery

Circular Shift Permutation Recovery

Kid is trapped in a strange computer game where the current checkpoint is reached only after restoring a permutation of the numbers \(1\) to \(n\) to ascending order. The computer allows kid to choose an integer \(x\) (the segment length) and then, in each subsequent move, kid can perform a circular shift operation on any consecutive segment of length \(x\). In a left circular shift, a segment \( [a_1, a_2, \dots, a_{x}] \) becomes \( [a_2, a_3, \dots, a_{x}, a_1] \); in a right circular shift it becomes \( [a_{x}, a_1, a_2, \dots, a_{x-1}] \). However, if the number of operations exceeds \(23 \times n\), the permutation will "explode" and kid will be doomed.

Your task is to provide kid with a restoration strategy to sort the permutation in at most \(23\,n\) operations. For simplicity, you can choose \(x = 2\). With \(x = 2\), a left (or right) circular shift on a segment of two elements simply swaps the two adjacent numbers. Thus, you can use adjacent swaps to sort the permutation. Note that the input given is a permutation of \(1, 2, \dots, n\) and you must output the chosen \(x\) value, followed by the number of operations, and then the list of operations. Each operation is described by a letter ('L' for left shift or 'R' for right shift) and the starting index (1-indexed) of the segment to be shifted.

Formally:

  • Input: The first line contains an integer \(n\) (the size of the permutation). The second line contains \(n\) space-separated integers, representing a permutation of \(1, 2, \dots, n\).
  • Output: The first line should print the chosen segment length \(x\) (we will use \(x = 2\)). The second line should contain an integer \(m\) (the number of operations). The following \(m\) lines each should contain an operation in the format "D pos" where D is either 'L' (for left shift) or 'R' (for right shift) and pos is the 1-indexed starting position of the segment on which the operation is applied.

The provided solution must sort the permutation using at most \(23\,n\) operations.

inputFormat

The input starts with an integer \(n\) \( (1 \le n \le 50)\) representing the length of the permutation. The second line contains \(n\) space-separated integers that form a permutation of \(1, 2, \dots, n\).

outputFormat

Output consists of multiple lines. The first line is the chosen segment length \(x\) (which should be 2 in our solution). The second line is an integer \(m\) representing the number of operations. Each of the next \(m\) lines contains an operation formatted as "D pos" where D is either 'L' or 'R' and pos is the starting index (1-indexed) of the segment that is circularly shifted.

If the permutation is already sorted, output \(m = 0\) (i.e. no operations are needed).

sample

3
1 2 3
2

0

</p>