Codeforces Global Round 13
G
Switch and Flip
Codeforces
2800
原题
← FH →
翻译中…

There are $n$ coins labeled from $1$ to $n$. Initially, coin $c_i$ is on position $i$ and is facing upwards (($c_1, c_2, \dots, c_n)$ is a permutation of numbers from $1$ to $n$). You can do some operations on these coins.

翻译中…

In one operation, you can do the following:

翻译中…
  • Choose $2$ distinct indices $i$ and $j$.
  • Then, swap the coins on positions $i$ and $j$.
  • Then, flip both coins on positions $i$ and $j$. (If they are initially faced up, they will be faced down after the operation and vice versa)
翻译中…

Construct a sequence of at most $n+1$ operations such that after performing all these operations the coin $i$ will be on position $i$ at the end, facing up.

翻译中…

Note that you do not need to minimize the number of operations.

翻译中…

Input

翻译中…

The first line contains an integer $n$ ($3 \leq n \leq 2 \cdot 10^5$) — the number of coins.

翻译中…

The second line contains $n$ integers $c_1,c_2,\dots,c_n$ ($1 \le c_i \le n$, $c_i \neq c_j$ for $i\neq j$).

翻译中…

Output

翻译中…

In the first line, output an integer $q$ $(0 \leq q \leq n+1)$ — the number of operations you used.

翻译中…

In the following $q$ lines, output two integers $i$ and $j$ $(1 \leq i, j \leq n, i \ne j)$ — the positions you chose for the current operation.

翻译中…

Note

翻译中…

Let coin $i$ facing upwards be denoted as $i$ and coin $i$ facing downwards be denoted as $-i$.

翻译中…

The series of moves performed in the first sample changes the coins as such:

翻译中…
  • $[~~~2,~~~1,~~~3]$
  • $[-3,~~~1,-2]$
  • $[-3,~~~2,-1]$
  • $[~~~1,~~~2,~~~3]$
翻译中…

In the second sample, the coins are already in their correct positions so there is no need to swap.

翻译中…

样例

Input #1
3
2 1 3
Output #1
3
1 3
3 2
3 1
InputCopy OutputCopy
Input #2
5
1 2 3 4 5
Output #2
0
InputCopy OutputCopy
题目信息
题号
1491G
时间限制
1 s
内存限制
256 MB
标签
构造
图论
数学

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。