Problem:
Score : 1200 points

Problem Statement

You are given a permutation of 1,2,...,N: p_1,p_2,...,p_N. Determine if the state where p_i=i for every i can be reached by performing the following operation any number of times:

Choose three elements p_{i-1},p_{i},p_{i+1} (2\leq i\leq N-1) such that p_{i-1}>p_{i}>p_{i+1} and reverse the order of these three.

Constraints

3 \leq N \leq 3 × 10^5

p_1,p_2,...,p_N is a permutation of 1,2,...,N.

Input

Input is given from Standard Input in the following format:

N
p_1
:
p_N

Output

If the state where p_i=i for every i can be reached by performing the operation, print Yes; otherwise, print No.

Sample Input 1

5
5
2
1
4
3

Sample Output 1

Yes

The state where p_i=i for every i can be reached as follows:

Reverse the order of p_1,p_2,p_3. The sequence p becomes 1,2,5,4,3.

Reverse the order of p_3,p_4,p_5. The sequence p becomes 1,2,3,4,5.

Sample Input 2

4
3
2
4
1

Sample Output 2

No

Sample Input 3

7
3
2
1
6
5
4
7

Sample Output 3

Yes

Sample Input 4

6
5
3
4
1
2
6

Sample Output 4

No

Solution in C++14 (GCC 5.4.1) (judge verdict: Runtime Error)

#include <bits/stdc++.h>

#define FOR(i, a, b) for(int i = a;i < b;i++)
#define For(i, a) FOR(i, 0, a)
#define REV(i, a, b) for(int i = b-1;i >= a;i--)
#define Rev(i, a) REV(i, 0, a)
#define REP(a) For(i, a)
#define DEBUG_PRINT(...) fprintf(stderr, __VA_ARGS__)

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef pair<int, int> pi;
typedef vector<int> vi;
void print_pair(pi a){
  cerr<<"("<<a.first<<", "<<a.second<<") ";
}
template<int T1, int T2>
void print_2d(int a[T1][T2], int x, int y){
  For(i, x){
    For(j, y){
      cerr<<a[i][j]<<" ";
    }
    cerr<<endl;
  }
}
template<class T>
void print_array(T a[],size_t n){
  cerr<<"array:";
  REP(n-1)cerr<<a[i]<<",";
  cerr<<a[n-1]<<endl;
}

int N;
int A[300001];
int tmp[300001];

int cnt_bubble(int b,int e){
  if(b==e)return 0;
  if(b+2==e){
    if(A[b]>A[e]){
      swap(A[b],A[e]);
      return 1;
    }
    return 0;
  }
  int mid=(b+e)/2;
  if((mid-e)%2==1)mid--;
  int ret=0;
  int l=cnt_bubble(b,mid);
  int r=cnt_bubble(mid+2,e);
  ret=l+r;
  int ln=(mid-b)/2+1,rn=(e-mid)/2;
  int nn=ln+rn;
  int i=0;
  int bb=b,mm=mid+2;
  int llnn=ln;
  while(i<nn){
    if(bb>mid){
      tmp[i]=A[mm];
      i++;
      mm+=2;
    }else if(mm>e){
      tmp[i]=A[bb];
      i++;
      bb+=2;
    }else if(A[bb]>A[mm]){
      tmp[i]=A[mm];
      cerr<<"tmp["<<i<<"]=A["<<mm<<"]"<<endl;
      ret+=llnn;
      mm+=2;
      i++;
    }else if(A[bb]<A[mm]){
      tmp[i]=A[bb];
      cerr<<"tmp["<<i<<"]=A["<<bb<<"]"<<endl;
      llnn--;
      bb+=2;
      i++;
    }
  }
  For(ii,nn){
    if(b+(ii*2)>N){exit(0);}
    A[b+(ii*2)]=tmp[ii];
  }
  cerr<<"tmp:";
  For(ii,nn){
    cerr<<tmp[ii]<<" ";
  }
  cerr<<endl;
  return ret;
}

int main(){
  cin>>N;
  REP(N){
    cin>>A[i];
    A[i]--;
    if((A[i]%2)!=(i%2)){
      cout<<"No"<<endl;
      return 0;
    }
  }
  int i=0;
  while(i<N){
    int b=i,e=A[i];
    int j=i;
    while(true){
      e=max(e,A[j]);
      if(j>e)goto NO;
      if(j==e)break;
      j++;
    }
    if((e-b)%2==1)
      goto NO;
    if(b==e){
      i=e+1;
      continue;
    }
    int cnt=0;
    for (int j = b+1; j < e; j+=2) {
      if(A[j]!=j)
        goto NO;
      if(A[j-1]>A[j]&&A[j]>A[j+1])cnt++;
    }
    For(ii,e-b+1){
      cerr<<A[ii]<<" ";
    }
    cerr<<endl;
    if(cnt!=1)goto NO;
    cnt=cnt_bubble(b,e);
    cerr<<"("<<b<<","<<e<<")"<<cnt<<":";
    For(ii,e-b+1){
      cerr<<A[ii]<<" ";
    }
    cerr<<endl;
    if(cnt!=(e-b)/2)
      goto NO;
    i=e+1;
  }
  cout<<"Yes"<<endl;
  return 0;
NO:
  cout<<"No"<<endl;
  return 0;
}