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: Accepted)

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cassert>
#include <vector>
using namespace std;

#define FILL(a, n, x) memset((a), (x), sizeof ((a)[0]) * (n))
#define FILLA(a, x) memset((a), (x), sizeof (a))
#define COPY(a, n, b) memcpy((b), (a), sizeof ((a)[0]) * (n))
#define COPYA(a, b) memcpy((b), (a), sizeof (a))

typedef unsigned long long lu;
typedef long long li;
typedef pair<int, int> pii;
const int mod = 1e9 + 7;

inline int Add(int x) { return x >= mod ? x - mod : x; }
inline void Add(int &x, int y) { x += y; if (x >= mod) x -= mod; }
inline int Sub(int x) { return x < 0 ? x + mod : x; }
inline void Sub(int &x, int y) { x = Sub(x - y); }
inline int Mul(int x, int y) { return (int)((lu)x * y % mod); }
inline int Mul(int x, int y, int z) { return (int)((lu)x * y % mod * z % mod); }

int Pow(int x, int y) {
  int z = 1;
  for (; y; y >>= 1) {
    if (y & 1) z = Mul(z, x);
    x = Mul(x, x);
  }
  return z;
}

int Inv(int x) { return Pow(x, mod - 2); }
template <class T> inline void Max(T &x, T y) { if (y > x) x = y; }
template <class T> inline void Min(T &x, T y) { if (y < x) x = y; }

// ----------------------------------------

const int maxn = 3e5;
int n, p[maxn];

void Assert(bool x) {
  if (!x) {
    puts("No");
    exit(0);
  }
}

void Check(int l, int r) {
  Assert(r - l & 1);
  for (int i = l; i < r; ++i) {
    Assert((i - l & 1) == (i == p[i]));
  }
  vector<int> L, R;
  for (int i = l; i < r; ++i) {
    if (i < p[i]) L.push_back(p[i]);
    if (i > p[i]) R.push_back(p[i]);
  }
  for (int i = 1; i < L.size(); ++i) {
    Assert(L[i - 1] < L[i]);
  }
  for (int i = 1; i < R.size(); ++i) {
    Assert(R[i - 1] < R[i]);
  }

  int min_p = n, max_p = -1;
  for (int i = l; i < r; ++i) {
    Min(min_p, p[i]);
    Max(max_p, p[i]);
  }
  Assert(min_p == l && max_p == r - 1);
}

int main(void) {
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) {
    scanf("%d", p + i);
    --p[i];
  }
  for (int i = 0; i < n; ) {
    if (i == p[i]) {
      ++i;
      continue;
    }
    int j = i + 1;
    while (j + 1 < n && (p[j] == j) && (p[j + 1] != j + 1)) {
      j += 2;
    }
    Check(i, j);
    i = j;
  }
  puts("Yes");
  return 0;
}