#P16548. [ICPC 2026 LAC] Booksort

[ICPC 2026 LAC] Booksort

题目描述

Beatriz always enjoyed reading, so she decided to open a bookstore to have books around her all day long. She wants to be sure that the books are properly organized, so she can attract many customers.

On a large shelf of the bookstore there are NN stacks of books in a row, with the stacks numbered from 11 to NN left to right. Stack ii contains AiA_i books. Beatriz would like the numbers of books to be in non-decreasing order, that is, AiAi+1A_i \le A_{i+1} for i=1,2,,N1i = 1, 2, \ldots, N - 1, which might require some rearrangement of the books.

However, Beatriz is lazy and she really does not feel like organizing the books by herself. Then she asked her best friend Bernardo for help. They agreed that Beatriz will give Bernardo a sequence of commands. In each command Beatriz will specify two distinct stacks ii and jj, and Bernardo will take the s=Ai+Ajs = A_i + A_j books in those stacks, rearranging them as evenly as possible. This means that after performing the command, the number of books in those stacks will be updated in the following way:

$$\begin{aligned} A_i &= \left\lfloor \frac{s}{2} \right\rfloor, \\ A_j &= \left\lceil \frac{s}{2} \right\rceil. \end{aligned}$$

Beatriz does not want to spend a lot of time with Bernardo moving around the books for her. She wants a sequence of at most 10510^5 commands that yields the desired non-decreasing order. But, you know, Beatriz does not want to decide the commands by herself. Can you prepare any valid sequence of commands for her?

输入格式

The first line contains an integer NN (2N50002 \le N \le 5000) indicating the number of book stacks on the shelf. Each stack is identified by a distinct integer from 11 to NN.

The second line contains NN integers A1,A2,,ANA_1, A_2, \ldots, A_N (1Ai1051 \le A_i \le 10^5), where AiA_i is the initial number of books in stack ii.

输出格式

The first line must contain an integer kk (0k1050 \le k \le 10^5) indicating the number of commands to perform.

Each of the next kk lines must describe a command with two integers ii and jj (1i,jN1 \le i, j \le N and iji \neq j), representing that the books in stacks ii and jj are rearranged as described. After performing all the commands in the order in which they appear in your answer, the shelf must be sorted in non-decreasing order.

It can be proven that a valid answer exists under the given constraints. If there are multiple solutions, output any of them; there is no need to minimize kk.

3
1 1 1
0
3
1 1 1
3
1 2
1 2
2 3
5
14 7 13 8 15
3
2 1
1 4
3 4

提示

Explanation of Sample 1:

Since the shelf is initially sorted in non-decreasing order, an empty sequence of commands is a valid output.

Explanation of Sample 2:

Although the shelf is initially sorted, the output is valid because the shelf ends sorted after at most 10510^5 commands. There is no need to minimize the number of commands.

Explanation of Sample 3:

The first command (i=2,j=1i=2, j=1) changes the shelf from [14,7,13,8,15\underline{14}, \underline{7}, 13, 8, 15] to [11,10,13,8,15\underline{11}, \underline{10}, 13, 8, 15].

The second command (i=1,j=4i=1, j=4) changes the shelf from [11,10,13,8,15\underline{11}, 10, 13, \underline{8}, 15] to [9,10,13,10,15\underline{9}, 10, 13, \underline{10}, 15].

The third command (i=3,j=4i=3, j=4) changes the shelf from [9,10,13,10,159, 10, \underline{13}, \underline{10}, 15] to [9,10,11,12,159, 10, \underline{11}, \underline{12}, 15], which is sorted in non-decreasing order.