201105: [AtCoder]ARC110 F - Esoswap

Memory Limit:1024 MB Time Limit:2 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

Score : $900$ points

Problem Statement

We have a permutation $P = P_0, P_1, \ldots, P_{N - 1}$ of $0, 1, \ldots, N - 1$.

You can do the following operation on $P$ at most $2 \times 10^5$ times:

  • Announce an integer $i ~ (0 \leq i \leq N - 1)$. Swap $P_i$ and $P_{(i + P_i) \textrm{ mod } N}$.

Your task is to sort $P$ in ascending order with this operation. If it is impossible, print -1 instead.

Constraints

  • All values in input are integers.
  • $2 \leq N \leq 100$
  • $P$ is a permutation of $0, 1, \ldots, N - 1$.

Input

Input is given from Standard Input in the following format:

$N$
$P_0$ $P_1$ $\ldots$ $P_{N - 1}$

Output

If it is impossible to sort $P$ in ascending order with at most $2 \times 10^5$ operations, print -1.

Otherwise, print $K + 1$ lines to represent a sequence of operations that sorts $P$ in ascending order, where $K$ is the number of operations done, as follows:

  • The first line should contain the integer $K$;
  • the $(1 + i)$-the line $(1 \leq i \leq K)$ should contain the integer $j ~ (0 \leq j \leq N - 1)$ announced in the $i$-th operation.

You do not have to minimize the number of operations done. If there are multiple such sequences of at most $2 \times 10^5$ operations, any of them will be accepted.


Sample Input 1

8
7 1 2 6 4 0 5 3

Sample Output 1

4
6
0
3
0

This sequence of operations sorts $P$ in ascending order, as follows:

  • First, announce $i = 6$. We swap $P_6 (= 5)$ and $P_{(6 + 5) \textrm{ mod } 8} = P_3 (= 6)$, turning $P$ into $7, 1, 2, 5, 4, 0, 6, 3$.

  • Then, announce $i = 0$. We swap $P_0 (= 7)$ and $P_{(0 + 7) \textrm{ mod } 8} = P_7 (= 3)$, turning $P$ into $3, 1, 2, 5, 4, 0, 6, 7$.

  • Then, announce $i = 3$. We swap $P_3 (= 5)$ and $P_{(3 + 5) \textrm{ mod } 8} = P_0 (= 3)$, turning $P$ into $5, 1, 2, 3, 4, 0, 6, 7$.

  • Finally, announce $i = 0$. We swap $P_0 (= 5)$ and $P_{(0 + 5) \textrm{ mod } 8} = P_5 (= 0)$, turning $P$ into $0, 1, 2, 3, 4, 5, 6, 7$.

Input

题意翻译

翻译: 给出一个整数 $N(2\leq N\leq 100)$ 和一个从 $0$ 开始的长度为 $N$ 的排列 $P=P_0,P_1,P_2\cdots P_{N-1}$,你可以进行以下操作: - 选择一个整数 $i(0\leq i \leq N-1)$。 - 交换当前数列上的 $P_{i}$ 与 $P_{(i\ +\ P_i)\ \textrm{\ mod\ }\ N}$ 两个位置上的数字。 求能否在 $2\times 10^5$ 之内构造出一种操作方案使得整个序列单调上升,如果有,则在第一行输出操作个数 $K$,接下来 $K$ 行,每行按顺序输出数列操作的位置 $i$,若没有,则输出 $-1$。 样例解释: 原序列为 $7,1,2,6,4,0,5,3$ 我们可以进行 $4$ 次操作: - 对位置 $6$ 进行操作则交换 $P_6\ (=5)$ 和 $P_{(6+5)\mod 8}=P_3\ (=6)$ 两个数则此时序列变为 $7, 1, 2, 5, 4, 0, 6, 3$ - 对位置 $0$ 进行操作则交换 $P_0\ (=7)$ 和 $P_{(0+7)\mod 8}=P_7\ (=3)$ 两个数则此时序列变为 $3, 1, 2, 5, 4, 0, 6, 7$ - 对位置 $3$ 进行操作则交换 $P_3\ (=5)$ 和 $P_{(3+5)\mod 8}=P_0\ (=3)$ 两个数则此时序列变为 $5, 1, 2, 3, 4, 0, 6, 7$ - 对位置 $0$ 进行操作则交换 $P_0\ (=5)$ 和 $P_{(0+5)\mod 8}=P_5\ (=0)$ 两个数则此时序列变为 $0, 1, 2, 3, 4, 5, 6, 7$,此时该数列有序。

加入题单

算法标签: