CF1375E.Inversion SwapSort

省选/NOI-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Madeline has an array aa of nn integers. A pair (u,v)(u, v) of integers forms an inversion in aa if:

  • 1≤u<v≤n1 \le u \lt v \le n.
  • au>ava_u \gt a_v.

Madeline recently found a magical paper, which allows her to write two indices uu and vv and swap the values aua_u and ava_v. Being bored, she decided to write a list of pairs (ui,vi)(u_i, v_i) with the following conditions:

  • all the pairs in the list are distinct and form an inversion in aa.
  • all the pairs that form an inversion in aa are in the list.
  • Starting from the given array, if you swap the values at indices u1u_1 and v1v_1, then the values at indices u2u_2 and v2v_2 and so on, then after all pairs are processed, the array aa 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.

玛德琳有一个包含 nn 个整数的数组 aa。若整数对 (u,v)(u, v) 满足以下条件,则称其为数组 aa 中的一个逆序对:

  • 1≤u<v≤n1 \le u \lt v \le n;
  • au>ava_u \gt a_v。

最近,玛德琳发现了一张神奇的纸,她可以在纸上写下两个下标 uu 和 vv,从而交换 aua_u 和 ava_v 的值。出于无聊,她决定写下一份整数对列表 (ui,vi)(u_i, v_i),该列表需满足以下条件:

  • 列表中所有整数对互不相同,且每个整数对均为 aa 中的一个逆序对;
  • aa 中所有逆序对均出现在该列表中;
  • 从给定的初始数组出发,依次执行交换操作:先交换下标 u1u_1 和 v1v_1 处的值,再交换下标 u2u_2 和 v2v_2 处的值,依此类推;当列表中所有整数对均处理完毕后,数组 aa 将变为非递减有序(即升序)。

请构造出满足上述条件的列表;若不存在这样的列表,请判定其不存在。若存在多个可行解,输出任意一个即可。

输入格式

The first line of the input contains a single integer nn (1≤n≤10001 \le n \le 1000) — the length of the array.

Next line contains nn integers a1,a2,...,ana_1,a_2,...,a_n (1≤ai≤109)(1 \le a_i \le 10^9) — elements of the array.

输入的第一行包含一个整数 nn(1≤n≤10001 \le n \le 1000)—— 表示数组的长度。

第二行包含 nn 个整数 a1,a2,...,ana_1,a_2,...,a_n(1≤ai≤1091 \le a_i \le 10^9)—— 表示数组的元素。

输出格式

Print -1 if no such list exists. Otherwise in the first line you should print a single integer mm (0≤m≤n(n−1)20 \le m \le \dfrac{n(n-1)}{2}) — number of pairs in the list.

The ii-th of the following mm lines should contain two integers ui,viu_i, v_i (1≤ui<vi≤n1 \le u_i \lt v_i\le n).

If there are multiple possible answers, you may find any of them.

如果不存在这样的列表,则输出 −1-1。否则,在第一行输出一个整数 mm(0≤m≤n(n−1)20 \le m \le \dfrac{n(n-1)}{2}),表示列表中数对的个数。

接下来的 mm 行中,第 ii 行应包含两个整数 ui,viu_i, v_i(1≤ui<vi≤n1 \le u_i \lt v_i \le 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][3,1,2] \rightarrow [2,1,3] \rightarrow [1,2,3].

In the second sample test case it will be [1,8,1,6]→[1,6,1,8]→[1,1,6,8][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.

在第一个样例测试用例中,数组的变化顺序为 [3,1,2]→[2,1,3]→[1,2,3][3,1,2] \rightarrow [2,1,3] \rightarrow [1,2,3]。

在第二个样例测试用例中,数组的变化顺序为 [1,8,1,6]→[1,6,1,8]→[1,1,6,8][1,8,1,6] \rightarrow [1,6,1,8] \rightarrow [1,1,6,8]。

在第三个样例测试用例中,数组已经排好序。

输入解题思路,AI测评打分。不知道怎么写?

首页