CF1909H.Parallel Swaps Sort

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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

The second line contains nn integers p1,p2,,pnp_1, p_2, \ldots, p_n ( 1pin1 \le p_i \le n , the pip_i are distinct) — the permutation before performing the operations.

输入格式

Output your operations in the following format.

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

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

After all the operations, ai=ia_i = i must be true for each ii ( 1in1 \leq i \leq n ).

输出格式

In the first test:

  • At the beginning, p=[2,5,4,1,3]p = [2, 5, 4, 1, 3] .
  • In the first operation, you can choose [l,r]=[1,4][l, r] = [1, 4] . Then, (a1,a2)(a_1, a_2) are swapped and (a3,a4)(a_3, a_4) are swapped. The new permutation is p=[5,2,1,4,3]p = [5, 2, 1, 4, 3] .
  • In the second operation, you can choose [l,r]=[1,2][l, r] = [1, 2] . Then, (a1,a2)(a_1, a_2) are swapped. The new permutation is p=[2,5,1,4,3]p = [2, 5, 1, 4, 3] .
  • In the third operation, you can choose [l,r]=[2,5][l, r] = [2, 5] . Then, (a2,a3)(a_2, a_3) are swapped and (a4,a5)(a_4, a_5) are swapped. The new permutation is p=[2,1,5,3,4]p = [2, 1, 5, 3, 4] .
  • In the fourth operation, you can choose [l,r]=[1,4][l, r] = [1, 4] . Then, (a1,a2)(a_1, a_2) are swapped and (a3,a4)(a_3, a_4) are swapped. The new permutation is p=[1,2,3,5,4]p = [1, 2, 3, 5, 4] .
  • In the fifth operation, you can choose [l,r]=[4,5][l, r] = [4, 5] . Then, (a4,a5)(a_4, a_5) are swapped. The new permutation is p=[1,2,3,4,5]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.

输入输出样例

  • 输入#1

    5
    2 5 4 1 3

    输出#1

    5
    1 4
    1 2
    2 5
    1 4
    4 5
  • 输入#2

    9
    1 2 3 4 5 6 7 8 9

    输出#2

    0
  • 输入#3

    10
    6 4 2 3 8 10 9 1 5 7

    输出#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

说明/提示

In the first test:

  • At the beginning, p=[2,5,4,1,3]p = [2, 5, 4, 1, 3] .
  • In the first operation, you can choose [l,r]=[1,4][l, r] = [1, 4] . Then, (a1,a2)(a_1, a_2) are swapped and (a3,a4)(a_3, a_4) are swapped. The new permutation is p=[5,2,1,4,3]p = [5, 2, 1, 4, 3] .
  • In the second operation, you can choose [l,r]=[1,2][l, r] = [1, 2] . Then, (a1,a2)(a_1, a_2) are swapped. The new permutation is p=[2,5,1,4,3]p = [2, 5, 1, 4, 3] .
  • In the third operation, you can choose [l,r]=[2,5][l, r] = [2, 5] . Then, (a2,a3)(a_2, a_3) are swapped and (a4,a5)(a_4, a_5) are swapped. The new permutation is p=[2,1,5,3,4]p = [2, 1, 5, 3, 4] .
  • In the fourth operation, you can choose [l,r]=[1,4][l, r] = [1, 4] . Then, (a1,a2)(a_1, a_2) are swapped and (a3,a4)(a_3, a_4) are swapped. The new permutation is p=[1,2,3,5,4]p = [1, 2, 3, 5, 4] .
  • In the fifth operation, you can choose [l,r]=[4,5][l, r] = [4, 5] . Then, (a4,a5)(a_4, a_5) are swapped. The new permutation is p=[1,2,3,4,5]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.

首页