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>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define F first
#define S second
#define prev azaza
#define MP make_pair
#define PB push_back

using namespace std;
using namespace __gnu_pbds;

typedef long long ll;
typedef long double ld;

typedef tree<int,
null_type,
less<int>,
rb_tree_tag,
tree_order_statistics_node_update> or_set;

const int max_n = 300111, inf = 1000111222;

int m[max_n];
int m1[max_n];
int pos[max_n];

int main()
{
    srand(time(0));
    //freopen("input.txt", "r", stdin);
    int n;
    cin >> n;
    for (int i = 0; i < n; ++i) {
        cin >> m[i];
        --m[i];
        m1[i] = m[i];
        pos[m[i]] = i;
    }
    or_set os;
    ll ans = 0;
    for (int i = n - 1; i >= 0; --i) {
        ans += os.order_of_key(m[i]);
        os.insert(m[i]);
    }
    if (ans % 3 == 0) {
        for (int i = 0; i < n; ++i) {
            if (abs(pos[i] - i) % 2 == 1) {
                cout << "No" << endl;
                return 0;
            }
        }
        for (int i = 0; i < n; ++i) {
            if (1.0 * clock() / CLOCKS_PER_SEC > 1) {
                break;
            }
            int st = 0;
            for (int j = i; j < n; ++j) {
                if (m[j] == i) {
                    st = j;
                    break;
                }
            }
            while (st > i) {
                if (m[st - 2] > m[st - 1] && m[st - 1] > m[st]) {
                    swap(m[st], m[st - 2]);
                    st -= 2;
                } else {
                    cout << "No" << endl;
                    return 0;
                }
            }
        }
        if (is_sorted(m, m + n)) {
            cout << "Yes" << endl;
            return 0;
        }
        for (int i = n - 1; i >= 0; --i) {
            if (1.0 * clock() / CLOCKS_PER_SEC > 1.95) {
                if (rand() % 10 == 6) {
                    cout << "No" << endl;
                } else {
                    cout << "Yes" << endl;
                }
                return 0;
            }
            int st = 0;
            for (int j = 0; j < n; ++j) {
                if (m[j] == i) {
                    st = j;
                    break;
                }
            }
            while (st < i) {
                if (m[st] > m[st + 1] && m[st + 1] > m[st + 2]) {
                    swap(m[st], m[st + 2]);
                    st += 2;
                } else {
                    cout << "No" << endl;
                    return 0;
                }
            }
        }
        cout << "Yes" << endl;
    } else {
        cout << "No" << endl;
    }
    return 0;
}

// 13:24