Problem:
Bozosort

　

Bozosort, as well as Bogosort, is a very inefficient sort algorithm. It is a random number-based algorithm that sorts sequence elements following the steps as below:

Randomly select two elements and swap them.

Verify if all the elements are sorted in increasing order.

Finish if sorted, else return to 1.

To analyze Bozosort, you have decided to simulate the process using several predetermined pairs of elements.

You are given several commands to swap two elements. Make a program to evaluate how many times you have to run the command before the sequence is aligned in increasing order.

Input

The input is given in the following format.

$N$
$a_1$ $a_2$ ... $a_N$
$Q$
$x_1$ $y_1$
$x_2$ $y_2$
$...$
$x_Q$ $y_Q$

The first line provides the number of sequence elements $N$ ($2 \leq N \leq 300,000$). The second line provides an array of integers $a_i$ ($1 \leq a_i \leq 10^9$) that constitutes the sequence. Each of the subsequent $Q$ lines provides a pair of integers $x_i,y_i$ ($1 \leq x_i,y_i \leq N$) that represent the $i$-th command, which swaps the two elements indicated by $x_i$ and $y_i$ ($x_i \ne y_i$).

Output

If neat alignment in increasing order is realized the first time after execution of multiple commands, output at what time it was. Output 0 if the initial sequence is aligned in increasing order, and -1 if exhaustive execution still failed to attain the goal.

Sample Input 1

6
9 7 5 6 3 1
3
1 6
2 5
3 4

Sample Output 1

2

Sample Input 2

4
4 3 2 1
2
1 2
3 4

Sample Output 2

-1

Sample Input 3

5
1 1 1 2 2
1
1 2

Sample Input 3

0

Solution in C++ (judge verdict: Accepted)

//Pck2018 BozoSort
//20181019
//F_Naru
#include<iostream>
using namespace std;
//正しい順序になったかをメモする配列と
//順序が正しい個数をメモする変数をつくる
//n番目のメモする配列が0かつ順序が正しかったら
//個数をメモする変数を１つ増やしn番目Nのメモする配列を1にする
int N;
int Sorted_num=0;

bool judge(int *, int*, int);
void output(int *);//debug
int main(){
  cin >> N;
  int* bozo = new int[N+2];
  int* sorted = new int[N+2];//bozo[i-1] <= bozo[i] なら 1 以外は 0
  int before = 0;//min
  bozo[0] = 0;//min
  bozo[N+1] = 1000000001;//Max
  sorted[0] = 1;
  sorted[N+1] = 1;

  for(int i=1; i<=N; i++){
    cin >> bozo[i];
    if(before <= bozo[i]){//正しい並び
      sorted[i] = 1;
      Sorted_num++;
    }
    before=bozo[i];
  }
  if(Sorted_num == N){//はじめからソートされている
    cout << 0 << endl;
    delete[] bozo;
    delete[] sorted;
    return 0;
  }
  //命令読み込み
  int Q,x,y,temp;
  cin >> Q;
  for(int i=1; i<=Q; i++){
    cin >> x >> y;
    //swap
    temp=bozo[y];
    bozo[y]=bozo[x];
    bozo[x]=temp;
    //判定
    judge(bozo, sorted, x);
    judge(bozo, sorted, y);
    if(Sorted_num >= N){
      cout << i << endl;
      delete[] bozo;
      delete[] sorted;
      return 0;
    }
  }
  cout << -1 << endl;
  delete[] bozo;
  delete[] sorted;
  return 0;
}

bool judge(int* a, int* b,  int x){//判定
  for(int i=0; i<2; i++){
    if(a[x-1+i] <= a[x+i]  && b[x+i] == 0){//swapにより正順になった
      b[x+i]=1;
      Sorted_num++;
    } else if(a[(x-1)+i] > a[x+i] && b[x+i] == 1){//swapによりダメな順になった
      b[x+i] = 0;
      Sorted_num--;
    }
  }
}

void output(int* p){//debug
  for(int i=0; i<=N+1; i++){
    cout << p[i] << " ";
  }
  cout << endl;
}