Pinely Round 3 (Div. 1 + Div. 2)
H
Parallel Swaps Sort
Codeforces
3500
原题
← GI →
翻译中…
翻译中…

You are given a permutation $p_1, p_2, \dots, p_n$ of $[1, 2, \dots, n]$. You can perform the following operation some (possibly $0$) times:

翻译中…
  • choose a subarray $[l, r]$ of even length;
  • swap $a_l$, $a_{l+1}$;
  • swap $a_{l+2}$, $a_{l+3}$ (if $l+3 \leq r$);
  • $\dots$
  • swap $a_{r-1}$, $a_r$.
翻译中…

Sort the permutation in at most $10^6$ operations. You do not need to minimize the number of operations.

翻译中…

Input

翻译中…

The first line contains a single integer $n$ ($2 \le n \le 3 \cdot 10^5$) — the length of the permutation.

翻译中…

The second line contains $n$ integers $p_1, p_2, \ldots, p_n$ ($1 \le p_i \le n$, the $p_i$ are distinct) — the permutation before performing the operations.

翻译中…

Output

翻译中…

Output your operations in the following format.

翻译中…

The first line should contain an integer $k$ ($0 \le k \le 10^6$) — the number of operations.

翻译中…

The next $k$ lines represent the $k$ operations in order. Each of these $k$ lines should contain two integers $l$ and $r$ ($1 \leq l \lt r \leq n$, $r-l+1$ must be even) — the corresponding operation consists in choosing the subarray $[l, r]$ and swapping its elements according to the problem statement.

翻译中…

After all the operations, $a_i = i$ must be true for each $i$ ($1 \leq i \leq n$).

翻译中…

Note

翻译中…

In the first test:

翻译中…
  • At the beginning, $p = [2, 5, 4, 1, 3]$.
  • In the first operation, you can choose $[l, r] = [1, 4]$. Then, $(a_1, a_2)$ are swapped and $(a_3, a_4)$ are swapped. The new permutation is $p = [5, 2, 1, 4, 3]$.
  • In the second operation, you can choose $[l, r] = [1, 2]$. Then, $(a_1, a_2)$ are swapped. The new permutation is $p = [2, 5, 1, 4, 3]$.
  • In the third operation, you can choose $[l, r] = [2, 5]$. Then, $(a_2, a_3)$ are swapped and $(a_4, a_5)$ are swapped. The new permutation is $p = [2, 1, 5, 3, 4]$.
  • In the fourth operation, you can choose $[l, r] = [1, 4]$. Then, $(a_1, a_2)$ are swapped and $(a_3, a_4)$ are swapped. The new permutation is $p = [1, 2, 3, 5, 4]$.
  • In the fifth operation, you can choose $[l, r] = [4, 5]$. Then, $(a_4, a_5)$ are swapped. The new permutation is $p = [1, 2, 3, 4, 5]$, which is sorted.
翻译中…

In the second test, the permutation is already sorted, so you do not need to perform any operation.

翻译中…

样例

Input #1
5
2 5 4 1 3
Output #1
5
1 4
1 2
2 5
1 4
4 5
InputCopy OutputCopy
Input #2
9
1 2 3 4 5 6 7 8 9
Output #2
0
InputCopy OutputCopy
Input #3
10
6 4 2 3 8 10 9 1 5 7
Output #3
15
1 8
6 9
1 8
3 10
1 10
1 10
1 6
6 9
6 9
2 7
9 10
5 10
1 6
2 9
1 10
InputCopy OutputCopy
题目信息
题号
1909H
时间限制
7 s
内存限制
1024 MB
标签
构造
数据结构

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