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)

// This code is generated by [cargo-atcoder](https://github.com/tanakh/cargo-atcoder)
// Original source code:
/*
use proconio::input;
#[allow(unused_imports)]
use proconio::marker::*;
#[allow(unused_imports)]
use std::cmp::*;
#[allow(unused_imports)]
use std::collections::*;
#[allow(unused_imports)]
use std::f64::consts::*;

#[allow(unused)]
const INF: usize = std::usize::MAX / 4;
#[allow(unused)]
const M: usize = 1000000007;

fn main() {
    input! {
        n: usize,
        k: usize,
        p: [usize; n],
    }
    let mut sorted_count = vec![0; n];
    for i in 1..n {
        if p[i - 1] < p[i] {
            sorted_count[i] = sorted_count[i - 1] + 1;
        }
    }

    let m = n - k + 1;
    let mut sorted = 0;
    for i in 0..m {
        if sorted_count[i + k - 1] >= 3 {
            sorted += 1;
        }
    }
    // eprintln!("{} {:?}", sorted, sorted_count);

    let mut set = BTreeSet::new();
    for i in 0..k {
        set.insert(p[i]);
    }
    let mut min_k = vec![0; m];
    let mut max_k = vec![0; m];
    min_k[0] = *set.iter().nth(0).unwrap();
    max_k[0] = *set.iter().last().unwrap();
    for i in 1..m {
        set.remove(&p[i - 1]);
        set.insert(p[i + k - 1]);
        min_k[i] = *set.iter().nth(0).unwrap();
        max_k[i] = *set.iter().last().unwrap();
    }
    // eprintln!("{:?}", min_k);
    // eprintln!("{:?}", max_k);
    let mut result = 1;
    for i in 1..m {
        if p[i - 1] > min_k[i] || max_k[i] > p[i + k - 1] {
            result += 1;
        }
    }
    if sorted > 0 {
        result -= sorted - 1;
    }
    println!("{}", result);
}
*/

fn main() {
    let exe = "/tmp/binC5DE6843";
    std::io::Write::write_all(&mut std::fs::File::create(exe).unwrap(), &decode(BIN)).unwrap();
    std::fs::set_permissions(exe, std::os::unix::fs::PermissionsExt::from_mode(0o755)).unwrap();
    std::process::exit(std::process::Command::new(exe).status().unwrap().code().unwrap())
}

fn decode(v: &str) -> Vec<u8> {
    let mut ret = vec![];
    let mut buf = 0;
    let mut tbl = vec![64; 256];
    for i in 0..64 { tbl[TBL[i] as usize] = i as u8; }
    for (i, c) in v.bytes().filter_map(|c| { let c = tbl[c as usize]; if c < 64 { Some(c) } else { None } }).enumerate() {
        match i % 4 {
            0 => buf = c << 2,
            1 => { ret.push(buf | c >> 4); buf = c << 4; }
            2 => { ret.push(buf | c >> 2); buf = c << 6; }
            3 => ret.push(buf | c),
            _ => unreachable!(),
        }
    }
    ret
}

const TBL: &'static [u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
const BIN: &'static str = "<base64-encoded ELF executable removed>";