Under construction

Still being built. Things may change

Permutations

Interview

Time limit2sMemory limit1024 MB

Summary
Given an inversion table of a permutation, output the permutation's cycle notation with each cycle starting from its smallest element.
Level

Medium5 of 10

Topics
Array, Binary search, Implementation
Solved
No attempts yet

Problem

Let [n][n] denote the set {1,2,…,n}\{1, 2, \ldots, n\}. A permutation of order nn is a one-to-one mapping from the set [n][n] to the set [n][n]. For example, let π ⁣:[7]→[7]\pi \colon [7] \to [7] be a concrete permutation of order 77. We usually represent it with a table like this:

π=(12345673741652).\pi = \begin{pmatrix} 1 & 2 & 3 & 4 & 5 & 6 & 7 \\ 3 & 7 & 4 & 1 & 6 & 5 & 2\end{pmatrix}.

This means that π(1)=3\pi(1) = 3, π(2)=7\pi(2) = 7, π(3)=4\pi(3) = 4, and so on. Since the top row is always 1 2 3 … n1\ 2\ 3\ \ldots\ n, we usually omit it and write the permutation in the one-line notation:

π=3 7 4 1 6 5 2.\pi = 3\ 7\ 4\ 1\ 6\ 5\ 2.

The cycle notation is also very popular. We start with element 11 and determine its image π(1)=3\pi(1) = 3. Next, we take the element 33 and determine its image π(3)=4\pi(3) = 4. We keep doing this until we arrive back at the element 11. Indeed, π(4)=1\pi(4) = 1. The permutation π\pi contains the cycle (1 3 4)(1\ 3\ 4). Then we take the smallest number that has not yet appeared in any cycle, which is 22 in this case, and repeat the process. Eventually, we obtain the following:

π=(1 3 4) (2 7) (5 6).\pi = (1\ 3\ 4)\ (2\ 7)\ (5\ 6).

A permutation of order nn can also be represented by an inversion table b1 b2 b3 … bnb_1\ b_2\ b_3\ \ldots\ b_n (0≤bi≤n−i0 \leq b_i \leq n-i), where bib_i is the number of elements that are greater than ii and appear to the left of ii in the one-line notation. In our example, the inversion table is:

3 5 0 1 2 1 0.3\ 5\ 0\ 1\ 2\ 1\ 0.

Every permutation can be written in exactly one way as an inversion table, and every inversion table b1 b2 b3 … bnb_1\ b_2\ b_3\ \ldots\ b_n with 0≤bi≤n−i0 \leq b_i \leq n-i for all i∈[n]i \in [n] represents a valid permutation.

Write a program that converts an inversion table to its corresponding cycle notation.

Input

The first line contains an integer nn, the order of a permutation. The second line contains nn space-separated integers describing a valid inversion table.

Output

Output one line that contains the cycle notation of the permutation given by the inversion table. Each cycle is enclosed in parentheses. Cycles are separated by one space. The numbers within a cycle are separated by one space. The smallest element always comes first in each cycle. Cycles are ordered by their first element. See the examples.

Constraints

1≤n≤1051 \leq n \leq 10^5

Examples2

  1. Example 1

    Input
    7
    3 5 0 1 2 1 0
    
    Expected output
    (1 3 4) (2 7) (5 6)
    
  2. Example 2

    Input
    6
    4 2 1 1 0 0
    
    Expected output
    (1 5) (2 3) (4) (6)