#P6305. [eJOI 2018] 循环排序
[eJOI 2018] 循环排序
Description
Given a sequence of length , you may perform the following operation multiple times:
Choose distinct indices (where ), then move to index , move to index , , move to index , and move to index .
In other words, you may rotate the elements in the following order: $i_1 \rightarrow i_2 \rightarrow \cdots \rightarrow i_k \rightarrow i_1$.
For example: , , , , . After the operation, the sequence becomes .
Your task is to sort the entire sequence into non-decreasing order using the minimum number of operations. Note that, over all operations, the total number of chosen indices must not exceed . If there is no such method (no solution), output -1.
Input Format
The first line contains two integers, and .
The second line contains integers , representing the sequence .
Output Format
If there is no solution, output only -1.
Otherwise, output an integer on the first line (it may be , see Sample 3), representing the minimum number of operations needed.
Then output lines describing each operation.
For each operation, first output an integer , the number of indices chosen in this operation, then output integers on the next line.
When the number of operations is minimized, you may output any feasible solution.
5 5
3 2 3 1 1
1
5
1 4 2 3 5
4 3
2 1 4 3
-1
2 0
2 2
0
6 9
6 5 4 3 2 1
2
6
1 6 2 5 3 4
3
3 2 1
6 8
6 5 4 3 2 1
3
2
3 4
4
1 6 2 5
2
2 1
Hint
[Explanation for Sample 1]
You can sort the array using two operations and , but this will get WA, because your task is to minimize , and the optimal answer has .
One feasible method is $1 \rightarrow 4 \rightarrow 2 \rightarrow 3 \rightarrow 5 \rightarrow 1$, which is exactly the sample output.
[Explanation for Sample 2]
The minimum possible total number of chosen indices over all operations is (one feasible method is and ), so there is no solution.
[Explanation for Sample 3]
The array is already sorted, so no operation is needed.
Additional note: Samples 4 and 5 satisfy the constraints of Subtasks 6 and 7.
[Constraints]
For of the testdata: , , .
| Subtask ID | Score | Constraints |
|---|---|---|
| Samples | ||
| and | ||
| is a permutation, | ||
| is a permutation, | ||
| is a permutation | ||
| No special constraints |
Source: eJOI2018 Problem F "Cycle Sort".
Note: The translation comes from LOJ.
Translated by ChatGPT 5
京公网安备 11011102002149号