Problem:
Score : 200 points

Problem Statement

We have a sequence p = {p_1,\ p_2,\ ...,\ p_N} which is a permutation of {1,\ 2,\ ...,\ N}.

You can perform the following operation at most once: choose integers i and j (1 \leq i < j \leq N), and swap p_i and p_j. Note that you can also choose not to perform it.

Print YES if you can sort p in ascending order in this way, and NO otherwise.

Constraints

All values in input are integers.

2 \leq N \leq 50

p is a permutation of {1,\ 2,\ ...,\ N}.

Input

Input is given from Standard Input in the following format:

N
p_1 p_2 ... p_N

Output

Print YES if you can sort p in ascending order in the way stated in the problem statement, and NO otherwise.

Sample Input 1

5
5 2 3 4 1

Sample Output 1

YES

You can sort p in ascending order by swapping p_1 and p_5.

Sample Input 2

5
2 4 3 5 1

Sample Output 2

NO

In this case, swapping any two elements does not sort p in ascending order.

Sample Input 3

7
1 2 3 4 5 6 7

Sample Output 3

YES

p is already sorted in ascending order, so no operation is needed.

Solution in Rust (1.15.1) (judge verdict: Accepted)

fn main() {
    let mode = "b";
    if mode == "a" {
        a::solve();
    } else if mode == "b" {
        b::solve();
    }
}

mod b {
    use std;
    use std::io::Read;

    pub fn solve() {
        let mut buf = String::new();
        std::io::stdin().read_to_string(&mut buf).unwrap();

        let mut text = buf.split_whitespace();
        text.next();

        let input: Vec<i32> = text.map(|x| x.parse().unwrap()).collect();
        let res = if calc(input) { "YES" } else { "NO" };
        println!("{}", res);
    }
    fn calc(mut p: Vec<i32>) -> bool {
        let ng_point = match search_ng(&p) {
            Some(x) => x,
            None => return true,
        };
        let min_point = match search_min(&p, ng_point + 1) {
            Some(x) => x,
            None => return true,
        };
        p.swap(ng_point, min_point);
        is_sorted(p)
    }
    fn is_sorted(p: Vec<i32>) -> bool {
        for i in 0..(p.len() - 1) {
            if p[i] > p[i + 1] {
                return false;
            }
        }
        true
    }

    fn search_ng(p: &Vec<i32>) -> Option<usize> {
        for i in 0..(p.len() - 1) {
            if p[i] > p[i + 1] {
                return Some(i);
            }
        }
        None
    }

    fn search_min(p: &Vec<i32>, start: usize) -> Option<usize> {
        let mut min = std::i32::MAX;
        let mut point = None;
        for i in start..p.len() {
            if p[i] < min {
                min = p[i];
                point = Some(i);
            }
        }
        point
    }
}

mod a {
    use std;
    use std::io::Read;

    pub fn solve() {
        let mut buf = String::new();
        std::io::stdin().read_to_string(&mut buf).unwrap();

        let mut text = buf.split_whitespace();
        let a = text.next().unwrap().parse().unwrap();
        let b = text.next().unwrap().parse().unwrap();
        match calc(a, b) {
            None => println!("IMPOSSIBLE"),
            Some(x) => println!("{}", x),
        }
    }

    fn calc(a: i32, b: i32) -> Option<i32> {
        let total = a + b;
        if total & 0x1 == 0x1 {
            return None;
        }
        Some(total / 2)
    }

    #[cfg(test)]
    mod tests {
        use super::*;

        #[test]
        fn test1() {
            assert_eq!(calc(2, 16), Some(9));
        }
    }
}