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: Wrong Answer)

#include <bits/stdc++.h>

#define I inline
#define fi first
#define se second
#define R register
#define LL long long
#define mp make_pair
#define reg register int
#define pii pair<int,int>
#define fo(i, a, b) for(reg i = a; i <= b; i++)
#define fd(i, a, b) for(reg i = a; i >= b; i--)
#define cr const reg&
using namespace std;
const int inf = 2147483647;
const int N = 3e5 + 1;

I int _max(cr x, cr y) {return x > y ? x : y;}
I int _min(cr x, cr y) {return x < y ? x : y;}
I int read() {
	reg x = 0, f = 1; char ch = getchar();
	while(ch < '0' || ch > '9') {if(ch == '-') f = -1; ch = getchar();}
	while(ch >= '0' && ch <= '9') x = (x << 3) + (x << 1) + (ch ^ 48), ch = getchar();
	return x * f;
}
I void ptt(cr x) {if(x >= 10) ptt(x / 10); putchar(x % 10 + '0');}
I void put(cr x) {x < 0 ? putchar('-'), ptt(-x) : ptt(x);}
I void pr1(cr x) {put(x), putchar(' ');}
I void pr2(cr x) {put(x), puts("");}

int p[N];

bool check(cr l, cr r) {
	reg len = r - l + 1;
	if(len == 1) return 1;
	if(!(len & 1)) return 0;
	if(p[l] ^ r) return 0;
	for(reg i = l + 1; i <= r; i += 2) if(p[i] ^ i) return 0;
	reg tt = p[l];
	if(tt ^ r) {
		for(reg i = l; i <= tt; i += 2) if(p[i] ^ (tt - i + l)) return 0;
		if((p[tt + 2] ^ r) || (p[tt + 4] ^ l)) return 0;
		reg yy = tt + 2;
		for(reg i = tt + 6; i <= r; i += 2) {
			if(yy ^ p[i]) return 0;
			yy += 2;
		}
	} else {
		for(reg i = l + 2; i <= r; i += 2) if(p[i] ^ (i - 2)) return 0;
	} return 1;
}

int main() {
	reg n = read();
	fo(i, 1, n) p[i] = read();
	reg L = 1, mx = 0;
	fo(i, 1, n) {
		mx = _max(mx, p[i]);
		if(mx == i) {
			if(!check(L, i)) {puts("No"); return 0;}
			L = i + 1;
		}
	} puts("Yes");
	return 0;
}