CF1375E.Inversion SwapSort
省选/NOI-
通过率:0%
时间限制:2.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Madeline has an array a of n integers. A pair (u,v) of integers forms an inversion in a if:
- 1≤u<v≤n.
- au>av.
Madeline recently found a magical paper, which allows her to write two indices u and v and swap the values au and av. Being bored, she decided to write a list of pairs (ui,vi) 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 u1 and v1, then the values at indices u2 and v2 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.
玛德琳有一个包含 n 个整数的数组 a。若整数对 (u,v) 满足以下条件,则称其为数组 a 中的一个逆序对:
- 1≤u<v≤n;
- au>av。
最近,玛德琳发现了一张神奇的纸,她可以在纸上写下两个下标 u 和 v,从而交换 au 和 av 的值。出于无聊,她决定写下一份整数对列表 (ui,vi),该列表需满足以下条件:
- 列表中所有整数对互不相同,且每个整数对均为 a 中的一个逆序对;
- a 中所有逆序对均出现在该列表中;
- 从给定的初始数组出发,依次执行交换操作:先交换下标 u1 和 v1 处的值,再交换下标 u2 和 v2 处的值,依此类推;当列表中所有整数对均处理完毕后,数组 a 将变为非递减有序(即升序)。
请构造出满足上述条件的列表;若不存在这样的列表,请判定其不存在。若存在多个可行解,输出任意一个即可。
输入格式
The first line of the input contains a single integer n (1≤n≤1000) — the length of the array.
Next line contains n integers a1,a2,...,an (1≤ai≤109) — elements of the array.
输入的第一行包含一个整数 n(1≤n≤1000)—— 表示数组的长度。
第二行包含 n 个整数 a1,a2,...,an(1≤ai≤109)—— 表示数组的元素。
输出格式
Print -1 if no such list exists. Otherwise in the first line you should print a single integer m (0≤m≤2n(n−1)) — number of pairs in the list.
The i-th of the following m lines should contain two integers ui,vi (1≤ui<vi≤n).
If there are multiple possible answers, you may find any of them.
如果不存在这样的列表,则输出 −1。否则,在第一行输出一个整数 m(0≤m≤2n(n−1)),表示列表中数对的个数。
接下来的 m 行中,第 i 行应包含两个整数 ui,vi(1≤ui<vi≤n)。
如果存在多个可能的答案,输出任意一个即可。
输入输出样例
输入#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]→[2,1,3]→[1,2,3].
In the second sample test case it will be [1,8,1,6]→[1,6,1,8]→[1,1,6,8].
In the third sample test case the array is already sorted.
在第一个样例测试用例中,数组的变化顺序为 [3,1,2]→[2,1,3]→[1,2,3]。
在第二个样例测试用例中,数组的变化顺序为 [1,8,1,6]→[1,6,1,8]→[1,1,6,8]。
在第三个样例测试用例中,数组已经排好序。
输入解题思路,AI测评打分。不知道怎么写?