Under construction

Still being built. Things may change

Ukje and Mysophobia

Interview

Time limit2sMemory limit256 MB

Summary
Sort a permutation of 1 to N by repeatedly reversing intervals, using at most N*N reversals to make card i sit at position i.
Level

Medium6 of 10

Topics
Array, Sorting, Implementation, Greedy
Solved
No attempts yet

Problem

You are given NN cards in a row, holding each integer from 11 to NN exactly once. You may choose any interval [L,R][L, R] with 1≤L<R≤N1 \le L < R \le N and reverse the whole interval. For instance, reversing [2,5][2, 5] in [1,5,10,15,20,25][1, 5, 10, 15, 20, 25] yields [1,20,15,10,5,25][1, 20, 15, 10, 5, 25].

Number positions from the left. The target arrangement has number ii on card ii for every ii. Apply operations to maximize the number of cards that match the target arrangement. Every permutation can be sorted by reversals alone, so the maximum is always NN.

Input

The first line gives the number of cards NN (1≤N≤10001 \le N \le 1000).

The second line gives the initial arrangement as NN integers from left to right.

Output

Print the number of operations opop on the first line. It must satisfy op≤N×Nop \le N \times N.

On each of the next opop lines, print the interval [L,R][L, R] used, with LL and RR separated by a single space. If opop is 00, print nothing more. Applying all operations in order must produce the target arrangement.

Examples3

  1. Example 1

    Input
    5
    2 1 5 4 3
    
    Expected output
    2
    3 5
    1 2
    
  2. Example 2

    Input
    5
    5 2 1 4 3
    
    Expected output
    2
    1 3
    3 5
    
  3. Example 3

    Input
    1
    1
    
    Expected output
    0