CF1909H.Parallel Swaps Sort

NOI/NOI+/CTSC

通过率:0%

时间限制:7.00s

内存限制:1024MB

AC君温馨提醒

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

题目描述

Dubmood - Keygen 8

⠀

You are given a permutation p1,p2,…,pnp_1, p_2, \dots, p_n of [1,2,…,n][1, 2, \dots, n]. You can perform the following operation some (possibly 00) times:

  • choose a subarray [l,r][l, r] of even length;
  • swap ala_l, al+1a_{l+1};
  • swap al+2a_{l+2}, al+3a_{l+3} (if l+3≤rl+3 \leq r);
  • …\dots
  • swap ar−1a_{r-1}, ara_r.

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

Dubmood - Keygen 8

⠀

给你一个 [1,2,…,n][1, 2, \dots, n] 的排列 p1,p2,…,pnp_1, p_2, \dots, p_n。你可以执行以下操作若干次(可以为 00 次):

  • 选择一个长度为偶数的子数组 [l,r][l, r];
  • 交换 ala_l 与 al+1a_{l+1};
  • 交换 al+2a_{l+2} 与 al+3a_{l+3}(若 l+3≤rl+3 \leq r);
  • …\dots
  • 交换 ar−1a_{r-1} 与 ara_r。

请在至多 10610^6 次操作内将该排列排序。你无需最小化操作次数。

输入格式

The first line contains a single integer nn (2≤n≤3⋅1052 \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 (1≤pi≤n1 \le p_i \le n, the pip_i are distinct) — the permutation before performing the operations.

第一行包含一个整数 nn(2≤n≤3⋅1052 \le n \le 3 \cdot 10^5)—— 排列的长度。

第二行包含 nn 个整数 p1,p2,…,pnp_1, p_2, \ldots, p_n(1≤pi≤n1 \le p_i \le n,且所有 pip_i 互不相同)—— 执行操作前的排列。

输出格式

Output your operations in the following format.

The first line should contain an integer kk (0≤k≤1060 \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 (1≤l<r≤n1 \leq l \lt r \leq n, r−l+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 (1≤i≤n1 \leq i \leq n).

请按以下格式输出你的操作。

第一行应包含一个整数 kk(0≤k≤1060 \le k \le 10^6)—— 表示操作的总数。

接下来的 kk 行依次表示这 kk 个操作。每行应包含两个整数 ll 和 rr(1≤l<r≤n1 \leq l \lt r \leq n,且 r−l+1r-l+1 必须为偶数)—— 对应的操作即为选择子数组 [l,r][l, r],并按照题目描述的方式交换其元素。

所有操作完成后,必须满足对每个 ii(1≤i≤n1 \leq i \leq n)均有 ai=ia_i = i。

输入输出样例

  • 输入#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.

在第一个测试用例中:

  • 初始时,p=[2,5,4,1,3]p = [2, 5, 4, 1, 3]。
  • 在第一次操作中,可选择区间 [l,r]=[1,4][l, r] = [1, 4]。此时,(a1,a2)(a_1, a_2) 被交换,且 (a3,a4)(a_3, a_4) 被交换。得到的新排列为 p=[5,2,1,4,3]p = [5, 2, 1, 4, 3]。
  • 在第二次操作中,可选择区间 [l,r]=[1,2][l, r] = [1, 2]。此时,(a1,a2)(a_1, a_2) 被交换。得到的新排列为 p=[2,5,1,4,3]p = [2, 5, 1, 4, 3]。
  • 在第三次操作中,可选择区间 [l,r]=[2,5][l, r] = [2, 5]。此时,(a2,a3)(a_2, a_3) 被交换,且 (a4,a5)(a_4, a_5) 被交换。得到的新排列为 p=[2,1,5,3,4]p = [2, 1, 5, 3, 4]。
  • 在第四次操作中,可选择区间 [l,r]=[1,4][l, r] = [1, 4]。此时,(a1,a2)(a_1, a_2) 被交换,且 (a3,a4)(a_3, a_4) 被交换。得到的新排列为 p=[1,2,3,5,4]p = [1, 2, 3, 5, 4]。
  • 在第五次操作中,可选择区间 [l,r]=[4,5][l, r] = [4, 5]。此时,(a4,a5)(a_4, a_5) 被交换。得到的新排列为 p=[1,2,3,4,5]p = [1, 2, 3, 4, 5],即已排序。

在第二个测试用例中,排列本身已有序,因此无需执行任何操作。

输入解题思路,AI测评打分。不知道怎么写?

首页