A16440 | Parallel Swaps Sort
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
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.
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 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 < 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$ ).
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 < 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$ ).
输出格式
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.
- 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.
输入输出样例
输入 #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, $(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.
- 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.
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted