Rayan Programming Contest 2024 - Selection (Codeforces Round 989, Div. 1 + Div. 2)
D
Darius' Wisdom
Codeforces
1600
原题
← CE →
翻译中…

Darius the Great is constructing $n$ stone columns, each consisting of a base and between $0$, $1$, or $2$ inscription pieces stacked on top.

翻译中…

In each move, Darius can choose two columns $u$ and $v$ such that the difference in the number of inscriptions between these columns is exactly $1$, and transfer one inscription from the column with more inscriptions to the other one. It is guaranteed that at least one column contains exactly $1$ inscription.

翻译中…

image

翻译中…

Since beauty is the main pillar of historical buildings, Darius wants the columns to have ascending heights. To avoid excessive workers' efforts, he asks you to plan a sequence of at most $n$ moves to arrange the columns in non-decreasing order based on the number of inscriptions. Minimizing the number of moves is not required.

翻译中…

Input

翻译中…

The first line contains an integer $t$ — the number of test cases. ($1 \leq t \leq 3000$)

翻译中…

The first line of each test case contains an integer $n$ — the number of stone columns. ($1 \leq n \leq 2 \cdot 10^5$)

翻译中…

The second line contains $n$ integers $a_1, a_2, \ldots, a_n$, where $a_i \in \{0,1,2\}$ represents the initial number of inscriptions in the $i$-th column. It is guaranteed that at least one column has exactly $1$ inscription.

翻译中…

It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.

翻译中…

Output

翻译中…

For each test case, output an integer $k$ — the number of moves used to sort the columns. ($0 \leq k \leq n$)

翻译中…

Then, output $k$ lines, each containing two integers $u_i$ and $v_i$ ($1 \leq u_i, v_i \leq n$), representing the indices of the columns involved in the $i$-th move. During each move, it must hold that $|a_{u_i} - a_{v_i}| = 1$, and one inscription is transferred from the column with more inscriptions to the other.

翻译中…

It can be proven that a valid solution always exists under the given constraints.

翻译中…

Note

翻译中…

Columns state in the first test case:

翻译中…
  • Initial: $0, 2, 0, 1$
  • After the first move: $0, 1, 0, 2$
  • After the second move: $0, 0, 1, 2$
翻译中…

Columns state in the second test case:

翻译中…
  • Initial: $1, 2, 0$
  • After the first move: $0, 2, 1$
  • After the second move: $0, 1, 2$
翻译中…

In the third test case, the column heights are already sorted in ascending order.

翻译中…

样例

Input #1
3
4
0 2 0 1
3
1 2 0
6
0 1 1 2 2 2
Output #1
2
2 4
2 3
2
3 1
2 3
0
InputCopy OutputCopy
题目信息
题号
2034D
时间限制
2 s
内存限制
256 MB
标签
构造
贪心
模拟
排序

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。