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>
using namespace std;
typedef vector<int> vi;
typedef vector<vector<int> > vvi;
typedef vector<pair<int, int> > vpii;
typedef pair<int, int> pii;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
typedef vector<ll> vll;
#define INF 0x3f3f3f3f
#define MOD 998244353LL
#define EPSILON 0.00001
#define f first
#define s second
#define pb push_back
#define mp make_pair

#define FOR(i, a, b) for (int i=(a); i<=(signed)(b); i++)
#define F0R(i, a) for (int i=0; i<(signed)(a); i++)
#define RFOR(i, a, b) for (int i=(a); i >= b; i--)

#define MN 300005
int n;
int p[MN];

bool chk(int l, int r){
    //cout << "checking " << l << " " << r << "\n";
    if((r-l)%2 != 0) return false;
    //assert((r-l)%2 == 0);
    for(int i=l+1; i<=r-1; i+=2){
        if(p[i] != i) return false;
    }
    int cmx = -INF, cmn = INF;
    for(int i=l; i<=r; i+=2){
        if(p[i] > i){
            if(cmn < p[i]) return false;
            cmn = p[i];
        } else if(p[i] < i){
            if(cmx > p[i]) return false;
            cmx = p[i];
        }
    }
    return true;
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    cin >> n;
    FOR(i, 1, n) cin >> p[i];
    int l = 1;
    int cm = 0;
    FOR(r, 1, n){
        cm = max(cm, p[r]);

        if(cm == r){
            if(!chk(l, r)){
                cout << "No\n";
                return 0;
            }
            l = r+1;
        }
    }
    cout << "Yes\n";
    
    return 0;
}