CF1909H.Parallel Swaps Sort
普及/提高-
通过率:0%
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
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.
输入格式
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 ).
输出格式
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.
输入输出样例
输入#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.