#P16123. [USTCPC 2026] Kruskal Loves Strongly Connected Graph

    ID: 18116 远端评测题 500ms 512MiB 尝试: 0 已通过: 0 显示难度NOI/NOI+/CTS 上传者: 标签>Special Judge2026高校校赛

[USTCPC 2026] Kruskal Loves Strongly Connected Graph

Background

Please note that this problem has non-standard time and memory limits.

Due to differences in judge machine performance, the time limit has been adjusted to 0.5 s.

Kruskal-chan likes strongly connected graphs, and she is dedicated to exploring their strong connectivity on various graphs.

Problem Description

You are given a sequence {ai}\{a_i\} of length nn.

Define fi=max⁡1≤j<i∧aj<ai{fj+1}f_i=\max\limits_{1 \le j<i \land a_j<a_i}\{f_j+1\}. In particular, if there is no 1≤j<i1 \le j<i such that aj<aia_j<a_i, then fi=1f_i=1.

There is a directed edge between uu and vv if and only if u<vu<v, au<ava_u<a_v, and fu+1=fvf_u+1=f_v.

Kruskal-chan wants to add some directed edges u→vu \rightarrow v so that the new graph after adding them is strongly connected.

Please tell her the minimum number of edges that need to be added, and construct a solution.

Note: A graph is strongly connected if and only if for any 1≤u,v≤n1 \le u,v \le n, both a path from uu to vv and a path from vv to uu exist.

Input Format

The first line contains a positive integer nn (1≤n≤105)(1 \le n \le 10^5), denoting the length of the sequence.

The second line contains nn integers. The ii-th positive integer aia_i (1≤ai≤109)(1\le a_i\le 10^9) denotes the ii-th element of the sequence.

Output Format

The first line outputs an integer mm, denoting the minimum number of edges to add.

In the next mm lines, each line outputs two positive integers u,vu,v separated by a space, denoting an added directed edge u→vu \rightarrow v.

You need to ensure that 0≤m≤n0 \le m \le n and u,v∈[1,n]u,v \in [1,n].

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

Hint

Translated by ChatGPT 5