#P6305. Minimum Cyclic Rotation Sort
Minimum Cyclic Rotation Sort
Minimum Cyclic Rotation Sort
You are given a sequence of n integers \(\{a_i\}_{i=1}^{n}\). You are allowed to perform the following cyclic rotation operation any number of times:
Choose \(k\) distinct indices \(i_1, i_2, \dots, i_k\) (with \(1 \le i_j \le n\)) and then simultaneously perform the cyclic move
[ a_{i_1} \to a_{i_2},\quad a_{i_2} \to a_{i_3},\quad \dots, \quad a_{i_{k-1}} \to a_{i_k},\quad a_{i_k} \to a_{i_1}. ]
For example, if \(n=4\) and \(\{a_i\} = \{10, 20, 30, 40\}\), and you choose \(i_1=2, i_2=3, i_3=4\), then after the operation the sequence becomes \(\{10, 40, 20, 30\}\).
Your task is to sort the entire sequence in non-decreasing order by using as few operations as possible. However, across all operations the total number of indices used (i.e. the sum of chosen \(k\) values) must not exceed a given integer \(s\). If it is impossible to sort the sequence subject to this constraint, output -1
.
Note: When the sequence is already sorted, output a valid solution with 0 operations.
inputFormat
The first line contains two integers \(n\) and \(s\) (the length of the sequence and the maximum allowed total number of indices used).
The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) separated by spaces.
outputFormat
If it is impossible to sort the sequence under the constraint, output -1
.
Otherwise, on the first line output an integer \(m\) representing the number of operations. Then for each operation output a line containing an integer \(k\) (the number of indices chosen in that operation) followed by \(k\) space‐separated indices (1-indexed) indicating the cyclic rotation. The operations must together achieve the sorted sequence.
sample
3 3
30 10 20
1
3 1 3 2
</p>