Problem:
Score : 700 points

Problem Statement

Snuke has a permutation (P_0,P_1,\cdots,P_{N-1}) of (0,1,\cdots,N-1).

Now, he will perform the following operation exactly once:

Choose K consecutive elements in P and sort them in ascending order.

Find the number of permutations that can be produced as P after the operation.

Constraints

2 \leq N \leq 200000

2 \leq K \leq N

0 \leq P_i \leq N-1

P_0,P_1,\cdots,P_{N-1} are all different.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K
P_0 P_1 \cdots P_{N-1}

Output

Print the number of permutations that can be produced as P after the operation.

Sample Input 1

5 3
0 2 1 4 3

Sample Output 1

2

Two permutations can be produced as P after the operation: (0,1,2,4,3) and (0,2,1,3,4).

Sample Input 2

4 4
0 1 2 3

Sample Output 2

1

Sample Input 3

10 4
2 0 1 3 7 5 4 6 8 9

Sample Output 3

6

Solution in Rust (1.15.1) (judge verdict: Wrong Answer)

use std::io::{self, BufRead, BufWriter, Write};
use std::collections::BTreeSet;

fn solve<R, W>(reader: R, mut writer: W)
where
    R: BufRead,
    W: Write,
{
    let mut lines = reader.lines();
    let a_line = lines.next().unwrap().unwrap();
    let mut splited = a_line.trim().split_whitespace();
    let _: u32 = splited.next().unwrap().parse().unwrap();
    let k: usize = splited.next().unwrap().parse().unwrap();

    let a_line = lines.next().unwrap().unwrap();
    let mut splited = a_line.trim().split_whitespace();
    let p: Vec<i32> = splited.map(|x| x.parse().unwrap()).collect();

    
    let mut window: BTreeSet<i32> = BTreeSet::new();

    for i in 0..k {
        window.insert(p[i]);
    }

    let mut ans = 0;
    let mut plusone = false;
    let mut unsorted_count = unsorted_count(&p[0..k]);
    for i in 0..p.len() - k + 1 {
        let mut m0 = -1;
        let mut m1 = -1;
        if i > 0 {
            m1 = *window.iter().next_back().unwrap();
            window.remove(&p[i - 1]);
            window.insert(p[i + k - 1]);
            m0 = *window.iter().next().unwrap();
        if p[i + k - 2] < p[i + k - 1] {
            if unsorted_count > 0 {
                unsorted_count -= 1;
            }
        } else {
            unsorted_count = k - 1;
        }
        }

        if unsorted_count <= 0 {
            plusone = true;
            continue;
        }

        if i <= 0 {
            ans += 1;
            continue;
        }

        if ! (p[i - 1] < m0 && m1 < p[i + k - 1]) {
            ans += 1;
        }
    }

    if plusone {
        ans += 1;
    }

    writeln!(&mut writer, "{}", ans).unwrap();
}

fn unsorted_count(x: &[i32]) -> usize {
    let mut pre = x[0];
    for i in 1..x.len() {
        if pre >= x[i] {
            return i;
        }
        pre = x[i]
    }
    return 0;
}

fn main() {
    let stdio = io::stdin();
    let input = stdio.lock();

    let stdout = io::stdout();
    let output = BufWriter::new(stdout.lock());

    solve(input, output);
}

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

    const TESTS: &'static [(&'static [u8], &'static [u8])] = &[
        (
            br"5 3
0 2 1 4 3", br"2
",
        ),
        (
            br"4 4
0 1 2 3", br"1
",
        ),
        (
            br"10 4
2 0 1 3 7 5 4 6 8 9", br"6
",
        ),
    ];

    #[test]
    fn test_b() {
        for &(input, expected) in TESTS {
            let mut output = Vec::new();
            solve(input, &mut output);
            let expected_str = String::from_utf8(expected.to_vec()).unwrap();
            let output_str = String::from_utf8(output).unwrap();
            assert_eq!(expected_str, output_str);
        }
    }
}