CF1375E.Inversion SwapSort

普及/提高-

通过率:0%

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:

  • 1u<vn1 \le u < v \le n .
  • au>ava_u > 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.

输入格式

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

Next line contains nn integers a1,a2,...,ana_1,a_2,...,a_n (1ai109)(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 mm ( 0mn(n1)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 ( 1ui<vin1 \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][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.

首页