CF1909H.Parallel Swaps Sort
NOI/NOI+/CTSC
通过率:0%
时间限制:7.00s
内存限制:1024MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
⠀
You are given a permutation p1,p2,…,pn of [1,2,…,n]. You can perform the following operation some (possibly 0) times:
- choose a subarray [l,r] of even length;
- swap al, al+1;
- swap al+2, al+3 (if l+3≤r);
- …
- swap ar−1, ar.
Sort the permutation in at most 106 operations. You do not need to minimize the number of operations.
⠀
给你一个 [1,2,…,n] 的排列 p1,p2,…,pn。你可以执行以下操作若干次(可以为 0 次):
- 选择一个长度为偶数的子数组 [l,r];
- 交换 al 与 al+1;
- 交换 al+2 与 al+3(若 l+3≤r);
- …
- 交换 ar−1 与 ar。
请在至多 106 次操作内将该排列排序。你无需最小化操作次数。
输入格式
The first line contains a single integer n (2≤n≤3⋅105) — the length of the permutation.
The second line contains n integers p1,p2,…,pn (1≤pi≤n, the pi are distinct) — the permutation before performing the operations.
第一行包含一个整数 n(2≤n≤3⋅105)—— 排列的长度。
第二行包含 n 个整数 p1,p2,…,pn(1≤pi≤n,且所有 pi 互不相同)—— 执行操作前的排列。
输出格式
Output your operations in the following format.
The first line should contain an integer k (0≤k≤106) — 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≤l<r≤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, ai=i must be true for each i (1≤i≤n).
请按以下格式输出你的操作。
第一行应包含一个整数 k(0≤k≤106)—— 表示操作的总数。
接下来的 k 行依次表示这 k 个操作。每行应包含两个整数 l 和 r(1≤l<r≤n,且 r−l+1 必须为偶数)—— 对应的操作即为选择子数组 [l,r],并按照题目描述的方式交换其元素。
所有操作完成后,必须满足对每个 i(1≤i≤n)均有 ai=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].
- In the first operation, you can choose [l,r]=[1,4]. Then, (a1,a2) are swapped and (a3,a4) are swapped. The new permutation is p=[5,2,1,4,3].
- In the second operation, you can choose [l,r]=[1,2]. Then, (a1,a2) are swapped. The new permutation is p=[2,5,1,4,3].
- In the third operation, you can choose [l,r]=[2,5]. Then, (a2,a3) are swapped and (a4,a5) are swapped. The new permutation is p=[2,1,5,3,4].
- In the fourth operation, you can choose [l,r]=[1,4]. Then, (a1,a2) are swapped and (a3,a4) are swapped. The new permutation is p=[1,2,3,5,4].
- In the fifth operation, you can choose [l,r]=[4,5]. Then, (a4,a5) 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.
在第一个测试用例中:
- 初始时,p=[2,5,4,1,3]。
- 在第一次操作中,可选择区间 [l,r]=[1,4]。此时,(a1,a2) 被交换,且 (a3,a4) 被交换。得到的新排列为 p=[5,2,1,4,3]。
- 在第二次操作中,可选择区间 [l,r]=[1,2]。此时,(a1,a2) 被交换。得到的新排列为 p=[2,5,1,4,3]。
- 在第三次操作中,可选择区间 [l,r]=[2,5]。此时,(a2,a3) 被交换,且 (a4,a5) 被交换。得到的新排列为 p=[2,1,5,3,4]。
- 在第四次操作中,可选择区间 [l,r]=[1,4]。此时,(a1,a2) 被交换,且 (a3,a4) 被交换。得到的新排列为 p=[1,2,3,5,4]。
- 在第五次操作中,可选择区间 [l,r]=[4,5]。此时,(a4,a5) 被交换。得到的新排列为 p=[1,2,3,4,5],即已排序。
在第二个测试用例中,排列本身已有序,因此无需执行任何操作。
输入解题思路,AI测评打分。不知道怎么写?