#P9291. [ROI 2018] Quick sort

[ROI 2018] Quick sort

Background

Translated from ROI 2018 Day2 T2. Быстрая сортировка (Quick sort).

Problem Description

You are given a permutation [a1,a2,,an][a_1,a_2,\ldots,a_n] of length nn.

Define an operation S(l,r)S(l,r) as follows: rearrange the segment [al,al+1,,ar][a_l,a_{l+1},\ldots,a_r] into [al+1,al+3,,al,al+2,][a_{l+1},a_{l+3},\ldots,a_l,a_{l+2},\ldots].

For example: $[2, 4, 1, 5, 3, 6, 7, 8]\xrightarrow{S(2,6)} [2, 1, 3, 4, 5, 6, 7, 8]$, where the subarray [4,1,5,3,6][4, 1, 5, 3, 6] is rearranged into [1,3,4,5,6][1, 3, 4, 5, 6].

Given a permutation, find how many operations are needed to make it increasing, and output any valid sequence of operations. You do notnot need to minimize the number of operations, but you must ensure the number of operations is 15000\leq 15000.

Input Format

The first line contains an integer nn.

The second line contains nn integers, representing the permutation aa.

Output Format

The first line contains an integer kk, the number of operations you perform. It must satisfy 0k150000 \leq k \leq 15000.

In the next kk lines, describe your operations in order. For each step, output two integers LL, RR, meaning you apply the operation once to the segment aL,aL+1,,aRa_L,a_{L+1},\ldots,a_R. It must satisfy 1LRn1 \leq L \leq R \le n.

Note that you do not need to minimize kk. You only need to guarantee 0k150000 \le k \leq 15000. It is guaranteed that such a kk exists.

5
3 1 4 2 5
1
1 5
8
2 4 1 5 3 6 7 8
2
2 6
1 2

2
2 1
3
1 1
2 2
1 2

Hint

Let k0k_0 be the minimum number of operations.

For all testdata, 1n30001\leq n\leq 3000, 0k0150000\leq k_0\leq 15000, 1ain1\leq a_i\leq n, and all aia_i are distinct.

Subtask ID nn Special property
11 1n1001 \leq n \leq 100 k0=1k_0 = 1
22 None
33 1n10001 \leq n \leq 1000
44 1n30001 \leq n \leq 3000

Translated by ChatGPT 5