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 Rust (1.42.0) (judge verdict: Wrong Answer)

use proconio::{input, marker::Usize1};

trait OrdTools: Ord + Clone + Sized {
    fn chmin(&mut self, x: Self) {
        *self = std::cmp::min(self.clone(), x);
    }
    fn chmax(&mut self, x: Self) {
        *self = std::cmp::max(self.clone(), x);
    }
}

impl<T: Ord + Clone + Sized> OrdTools for T {}

fn err() -> ! {
    println!("No");
    std::process::exit(0);
}

trait Itertools2: Iterator {
    fn is_sorted2(self) -> bool
    where
        Self: Sized,
        Self::Item: PartialOrd,
    {
        self.is_sorted_by2(PartialOrd::partial_cmp)
    }

    fn is_sorted_by2<F>(mut self, mut compare: F) -> bool
    where
        Self: Sized,
        F: FnMut(&Self::Item, &Self::Item) -> Option<std::cmp::Ordering>,
    {
        let mut last = match self.next() {
            Some(e) => e,
            None => return true,
        };

        while let Some(curr) = self.next() {
            if let Some(std::cmp::Ordering::Greater) | None = compare(&last, &curr) {
                return false;
            }
            last = curr;
        }

        true
    }
}

impl<I: Iterator> Itertools2 for I {}

fn main() {
    input!(n: usize, p: [Usize1; n]);
    let mut l;
    let mut r = 0;
    let mut max = 0;
    while r < n {
        l = r;
        r = l + 1;
        max.chmax(p[l]);
        while r < n && r - 1 < max {
            max.chmax(p[r]);
            r += 1;
        }
        if (r - l) % 2 == 0
            || l + 1 != r
                && ((l..r).any(|i| (i - l) % 2 == 1 && i != p[i])
                    && (l..r).any(|i| (i - l) % 2 == 0 && i == p[i])
                    && !(l..r)
                        .filter(|&i| (i - l) % 2 == 0 && i < p[i])
                        .is_sorted2()
                    && !(l..r)
                        .filter(|&i| (i - l) % 2 == 0 && i > p[i])
                        .is_sorted2())
        {
            err();
        }
    }
    println!("YES");
}