A13643 | Inversion SwapSort
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
Madeline has an array $a$ of $n$ integers. A pair $(u, v)$ of integers forms an inversion in $a$ if:
- $1 \le u < v \le n$ .
- $a_u > a_v$ .
Madeline recently found a magical paper, which allows her to write two indices $u$ and $v$ and swap the values $a_u$ and $a_v$ . Being bored, she decided to write a list of pairs $(u_i, v_i)$ with the following conditions:
- all the pairs in the list are distinct and form an inversion in $a$ .
- all the pairs that form an inversion in $a$ are in the list.
- Starting from the given array, if you swap the values at indices $u_1$ and $v_1$ , then the values at indices $u_2$ and $v_2$ and so on, then after all pairs are processed, the array $a$ will be sorted in non-decreasing order.
Construct such a list or determine that no such list exists. If there are multiple possible answers, you may find any of them.
- $1 \le u < v \le n$ .
- $a_u > a_v$ .
Madeline recently found a magical paper, which allows her to write two indices $u$ and $v$ and swap the values $a_u$ and $a_v$ . Being bored, she decided to write a list of pairs $(u_i, v_i)$ with the following conditions:
- all the pairs in the list are distinct and form an inversion in $a$ .
- all the pairs that form an inversion in $a$ are in the list.
- Starting from the given array, if you swap the values at indices $u_1$ and $v_1$ , then the values at indices $u_2$ and $v_2$ and so on, then after all pairs are processed, the array $a$ will be sorted in non-decreasing order.
Construct such a list or determine that no such list exists. If there are multiple possible answers, you may find any of them.
输入格式
The first line of the input contains a single integer $n$ ( $1 \le n \le 1000$ ) — the length of the array.
Next line contains $n$ integers $a_1,a_2,...,a_n$ $(1 \le a_i \le 10^9)$ — elements of the array.
Next line contains $n$ integers $a_1,a_2,...,a_n$ $(1 \le a_i \le 10^9)$ — elements of the array.
输出格式
Print -1 if no such list exists. Otherwise in the first line you should print a single integer $m$ ( $0 \le m \le \dfrac{n(n-1)}{2}$ ) — number of pairs in the list.
The $i$ -th of the following $m$ lines should contain two integers $u_i, v_i$ ( $1 \le u_i < v_i\le n$ ).
If there are multiple possible answers, you may find any of them.
The $i$ -th of the following $m$ lines should contain two integers $u_i, v_i$ ( $1 \le u_i < v_i\le n$ ).
If there are multiple possible answers, you may find any of them.
输入输出样例
输入 #1
3 3 1 2
输出 #1
2 1 3 1 2
输入 #2
4 1 8 1 6
输出 #2
2 2 4 2 3
输入 #3
5 1 1 1 2 2
输出 #3
0
In the first sample test case the array will change in this order $[3,1,2] \rightarrow [2,1,3] \rightarrow [1,2,3]$ .
In the second sample test case it will be $[1,8,1,6] \rightarrow [1,6,1,8] \rightarrow [1,1,6,8]$ .
In the third sample test case the array is already sorted.
In the second sample test case it will be $[1,8,1,6] \rightarrow [1,6,1,8] \rightarrow [1,1,6,8]$ .
In the third sample test case the array is already sorted.
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted