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: Accepted)

use std::cmp::Ordering;
use std::collections::VecDeque;
use std::io::{stdin, Read};
use std::str::FromStr;

// https://qiita.com/tubo28/items/e6076e9040da57368845
fn read<T: FromStr>() -> T {
    let stdin = stdin();
    let stdin = stdin.lock();
    let token: String = stdin
        .bytes()
        .map(|c| c.expect("failed to read char") as char)
        .skip_while(|c| c.is_whitespace())
        .take_while(|c| !c.is_whitespace())
        .collect();
    token.parse().ok().expect("failed to parse token")
}

struct SlideMin<'a, T: 'a> {
    vs: &'a Vec<T>,
    k: usize,
    i: usize,
    end: usize,
    deq: VecDeque<usize>,
    ord: Ordering,
}

impl<'a, T: Ord> SlideMin<'a, T> {
    pub fn min(aa: &Vec<T>, k: usize) -> SlideMin<T> {
        return SlideMin::new(&aa, k, Ordering::Greater);
    }

    pub fn max(aa: &Vec<T>, k: usize) -> SlideMin<T> {
        return SlideMin::new(&aa, k, Ordering::Less);
    }

    pub fn new(aa: &Vec<T>, window: usize, ordering: Ordering) -> SlideMin<T> {
        let mut this = SlideMin {
            vs: aa,
            k: window,
            i: 0,
            end: aa.len() - window,
            deq: VecDeque::new(),
            ord: ordering,
        };

        for i in 0..window - 1 {
            this.push(i);
        }
        return this;
    }

    pub fn next(&mut self) -> Option<&T> {
        let i = self.i;
        let &k = &self.k;
        if i <= self.end {
            self.i += 1;
            self.push(i + k - 1);
            return self.pop(i);
        } else {
            return None;
        }
    }

    fn push(&mut self, i: usize) {
        while let Some(&x) = self.deq.back() {
            let cmp = self.vs[x].cmp(&self.vs[i]);
            if cmp == self.ord || cmp == Ordering::Equal {
                self.deq.pop_back();
            } else {
                break;
            }
        }
        self.deq.push_back(i)
    }

    fn pop(&mut self, i: usize) -> Option<&T> {
        while let Some(&x) = self.deq.front() {
            if x >= i {
                return Some(&self.vs[x]);
            } else {
                self.deq.pop_front();
            }
        }
        return None;
    }
}

#[allow(dead_code)]
fn main() {
    let n = read::<usize>();
    let k = read::<usize>();
    let pp = (0..n).map(|_| read::<usize>()).collect::<Vec<_>>();

    let mut s_min = SlideMin::min(&pp, k);
    let mut s_max = SlideMin::max(&pp, k);

    let mut prev_min = s_min.next().unwrap().to_owned();
    let mut prev_max = s_max.next().unwrap().to_owned();

    let mut ans: usize = 1;
    while let Some((min, max)) = next(&mut s_min, &mut s_max) {
        let same_with_prev = prev_min < min && prev_max < max;
        if !same_with_prev {
            ans += 1;
        }

        prev_min = min;
        prev_max = max;
    }

    let mut already_sorted = 0;
    let mut prev = pp[0];
    let mut count = 1;

    for i in 1..n {
        if prev < pp[i] {
            count += 1;
            if count == k {
                already_sorted += 1;
            }
        } else {
            count = 1;
        }
        prev = pp[i];
    }

    let ans = if already_sorted > 0 {
        ans - already_sorted + 1
    } else {
        ans
    };
    println!("{}", ans);

    fn next<'a>(
        s_min: &'a mut SlideMin<usize>,
        s_max: &'a mut SlideMin<usize>,
    ) -> Option<(usize, usize)> {
        if let Some(&a) = s_min.next() {
            if let Some(&b) = s_max.next() {
                return Some((a, b));
            }
        }
        return None;
    }
}