QOJ.ac

QOJ

Límite de tiempo: 2 s Límite de memoria: 512 MB Puntuación total: 100

#17801. Kruskal Loves Strongly Connected Graph

Estadísticas

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

Given a sequence $\{a_i\}$ of length $n$.

Let $f_i = \max_{1 \le j < i \land a_j < a_i} \{f_j + 1\}$. Specifically, if there is no $1 \le j < i$ such that $a_j < a_i$, then $f_i = 1$.

A directed edge exists between $u$ and $v$ if and only if $u < v$, $a_u < a_v$, and $f_u + 1 = f_v$.

Kruskal-chan wants to add some directed edges $u \to v$ such that the resulting graph is strongly connected.

Please tell her the minimum number of edges that need to be added and provide a construction.

Note: A graph is strongly connected if and only if for any $1 \le u, v \le n$, there exists a path from $u$ to $v$ and a path from $v$ to $u$.

Input

The first line contains a positive integer $n$ ($1 \le n \le 10^5$), representing the length of the sequence.

The second line contains $n$ integers, where the $i$-th positive integer $a_i$ ($1 \le a_i \le 10^9$) represents the $i$-th term of the sequence.

Output

The first line outputs an integer $m$, representing the minimum number of edges to add.

The next $m$ lines each output two space-separated positive integers $u, v$, representing the added directed edge $u \to v$.

It must be guaranteed that $0 \le m \le n$ and $u, v \in [1, n]$.

Examples

Input 1

6
2 1 3 5 4 1

Output 1

3
6 1
4 2
5 6

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.