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;
void check(vector<int> &v, int st, int fin){
    vector<int> good;
    set<int> unused;
    for (int i=st; i <= fin; i++) unused.insert(v[i]);
    int ind = fin;
    while (ind >= st){
        set<int>::iterator it = unused.end();
        it--;
        int num = *it;
        while (true){
            unused.erase(unused.find(v[ind]));
            if (v[ind] == num){
                good.push_back(ind);
                ind--;
                break;
            }
            ind--;
        }
    }
    reverse(good.begin(), good.end());
    int u = 0;
    for (int i=st; i <= fin; i++){
        if (good[u] == i){
            u++;
            continue;
        }
        int pl = i;
        int diff = i - st;
        if (diff%2 == 0) pl -= 2*u;
        pl++;
        if (pl == v[i]) continue;
        cout << "No";
        exit(0);
    }
}
void solve(vector<int> &v, int st){
    if (st == v.size()) return;
    set<int> ms;
    for (int i=st; i < v.size(); i++){
        ms.insert(v[i]-st);
        set<int>::iterator it = ms.end();
        it--;
        int mx = *(it);
        if (mx == ms.size()){
            check(v, st, i);
            solve(v, i+1);
            return;
        }
    }
}
signed main() {
    int n;
    cin >> n;
    vector<int> v(n);
    for (int i=0; i < n; i++) cin >> v[i];
    solve(v, 0);
    cout << "Yes";

}