#P16316. [ICPC 2023 Jinan R] 奇怪的排序

    ID: 18252 远端评测题 1000ms 1024MiB 尝试: 0 已通过: 0 显示难度普及 上传者: 标签>贪心2023Special Judge排序构造ICPC济南

[ICPC 2023 Jinan R] 奇怪的排序

Problem Description

:::epigraph[Stanley P. Y. Fung. Is this the simplest (and most surprising) sorting algorithm ever? arXiv:2110.01111] We present an extremely simple sorting algorithm. It looks obviously wrong, but we prove that it is actually correct. :::

After learning about the strange sorting algorithm in the problem "Paimon Sorting" from ICPC 2021 Asia Regional Nanjing Site, Xiaoqingyu came up with the following question.

Given a sequence a1,a2,⋯ ,ana_1, a_2, \cdots, a_n, which is a permutation of size nn, you need to sort it in non-decreasing order. To do this, you may perform the following operation at most ⌊n2⌋\left\lfloor \frac{n}{2} \right\rfloor times: choose two indices ll and rr such that 1≤l<r≤n1 \le l < r \le n and al>ara_l > a_r, then sort al,al+1,⋯ ,ara_l, a_{l + 1}, \cdots, a_r in non-decreasing order.

Recall that a permutation of size nn is a sequence of length nn in which each integer from 11 to nn (inclusive) appears exactly once. Also, ⌊x⌋\lfloor x \rfloor denotes the greatest integer not larger than xx.

Input Format

There are multiple test cases. The first line contains an integer TT, the number of test cases. For each test case:

The first line contains an integer nn (1≤n≤1001 \le n \le 100), the length of the permutation.

The second line contains nn distinct integers a1,a2,⋯ ,ana_1, a_2, \cdots, a_n (1≤ai≤n1 \le a_i \le n), representing the given permutation.

It is guaranteed that the sum of nn over all test cases does not exceed 10410^4.

Output Format

For each test case, first output one line with an integer kk (0≤k≤⌊n2⌋0 \le k \le \left\lfloor \frac{n}{2} \right\rfloor), the number of operations you perform. Then output kk lines, where the ii-th line contains two integers lil_i and rir_i separated by a single space, indicating the two indices you choose for the ii-th operation.

It can be proven that an answer always exists. If there are multiple valid answers, you may output any one of them.

3
6
2 3 4 6 5 1
5
1 2 3 4 5
3
2 3 1
2
3 6
1 3
0
1
1 3

Hint

For the first sample, after the first operation the permutation becomes {2,3,1,4,5,6}\{2, 3, 1, 4, 5, 6\}, and after the second operation it becomes {1,2,3,4,5,6}\{1, 2, 3, 4, 5, 6\}. At this time, the permutation is in non-decreasing order.

Translated by ChatGPT 5