#P6305. [eJOI 2018] 循环排序

[eJOI 2018] 循环排序

Description

Given a sequence {ai}\{a_i\} of length nn, you may perform the following operation multiple times:

Choose kk distinct indices i1,i2,iki_1, i_2 \cdots, i_k (where 1ijn1 \leq i_j \leq n), then move ai1a_{i_1} to index i2i_2, move ai2a_{i_2} to index i3i_3, \ldots, move aik1a_{i_{k-1}} to index iki_k, and move aika_{i_k} to index i1i_1.

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: n=4n = 4, {ai}={10,20,30,40}\{a_i\} = \{10, 20, 30, 40\}, i1=2i_1 = 2, i2=3i_2 = 3, i3=4i_3 = 4. After the operation, the sequence aa becomes {10,40,20,30}\{10, 40, 20, 30\}.

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 ss. If there is no such method (no solution), output -1.

Input Format

The first line contains two integers, nn and ss.

The second line contains nn integers a1na_{1\cdots n}, representing the sequence {ai}\{a_i\}.

Output Format

If there is no solution, output only -1.

Otherwise, output an integer qq on the first line (it may be 00, see Sample 3), representing the minimum number of operations needed.

Then output 2×q2 \times q lines describing each operation.

For each operation, first output an integer kk, the number of indices chosen in this operation, then output kk integers i1ki_{1\cdots k} on the next line.

When the number of operations qq 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 1411 \rightarrow 4 \rightarrow 1 and 23522 \rightarrow 3 \rightarrow 5 \rightarrow 2, but this will get WA, because your task is to minimize qq, and the optimal answer has q=1q = 1.

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 44 (one feasible method is 1211 \rightarrow 2 \rightarrow 1 and 3433 \rightarrow 4 \rightarrow 3), 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 100%100\% of the testdata: 1n2×1051 \leq n \leq 2 \times 10^5, 0s2×1050 \leq s \leq 2 \times 10^5, 1ai1091 \leq a_i \leq 10^9.

Subtask ID Score Constraints
11 00 Samples
22 55 n,s2n, s \leq 2 and 1ai21 \leq a_i \leq 2
33 n5n \leq 5
44 1ai21 \leq a_i \leq 2
55 1010 aa is a permutation, s=2×ns = 2 \times n
66 aa is a permutation, n103n \leq 10^3
77 1515 aa is a permutation
88 s=2×ns = 2 \times n
99 n103n \leq 10^3
1010 2020 No special constraints

Source: eJOI2018 Problem F "Cycle Sort".

Note: The translation comes from LOJ.

Translated by ChatGPT 5