This page is still under construction.

Parts of this page are still being built. What you see may change.

Sorting 2

Time limit1sMemory limit512 MB

Summary
Choose one swap per round after the fixed rival swap to sort the permutation in the fewest rounds, with the lexicographically smallest choice list on ties.
Level

Hard8 of 10

Topics
BFS, Shortest path, Sorting
Solved
No attempts yet

Problem

Aizan has a sequence SS of length NN that contains each of the integers 0 to N−1N-1 exactly once. Aizan wants to sort it into increasing order by swapping the values at two positions.

Ermac, an old friend of Aizan, also swaps the values at two positions of the same sequence. Ermac picks the positions at random and has no intention of sorting anything.

The two of them repeat rounds. In one round Ermac swaps first, then Aizan swaps. A swap means choosing two positions and exchanging the values stored there. The two chosen positions may be equal, and then the sequence stays as it is.

Aizan knows Ermac's whole plan before the first round starts. The plan covers MM rounds numbered 0 to M−1M-1. In round ii Ermac swaps the values at positions X[i]X[i] and Y[i]Y[i].

Before a round starts, if the sequence is already in increasing order, Aizan stops there. Let RR be the number of rounds played up to that point. If SS is already sorted at the beginning, then R=0R = 0. If the sequence is sorted right after Ermac's swap in some round, Aizan can choose the same position twice, and the sequence is sorted at the end of that round.

You are given the sequence SS and Ermac's plan MM, XX, YY. Find the two positions Aizan chooses in each round so that RR is as small as possible.

Input

The first line contains the length NN of the sequence.

The second line contains S[0]S[0] to S[N−1]S[N-1], separated by spaces.

The third line contains the number of rounds MM that Ermac planned.

The ii-th of the next MM lines contains X[i]X[i] and Y[i]Y[i], separated by a space.

  • 1≤N≤5001 \le N \le 500
  • SS contains each integer from 0 to N−1N-1 exactly once
  • 1≤M≤10001 \le M \le 1000
  • 0≤X[i],Y[i]≤N−10 \le X[i], Y[i] \le N-1
  • Aizan can always sort the sequence within MM rounds

Output

On the first line, print the smallest possible number of rounds RR.

On each of the next RR lines, print the positions Aizan chooses. The ii-th of those lines contains the two positions P[i]P[i] and Q[i]Q[i] chosen in round ii, separated by a space, with 0≤P[i]≤Q[i]≤N−10 \le P[i] \le Q[i] \le N-1. Writing P[i]=Q[i]P[i] = Q[i] means a swap that leaves the sequence unchanged.

If several answers reach the minimum RR, print the one whose sequence P[0],Q[0],P[1],Q[1],…,P[R−1],Q[R−1]P[0], Q[0], P[1], Q[1], \dots, P[R-1], Q[R-1], read in that order, is lexicographically smallest.

If RR is 0, print only the first line.

Examples5

  1. Example 1

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

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

    Input
    1
    0
    1
    0 0
    
    Expected output
    0
    
  4. Example 4

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

    Input
    3
    1 0 2
    2
    0 1
    2 2
    
    Expected output
    1
    0 0