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 C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<ll, ll>;
const ll INF = 1e18;
#define print(x) cout << (x) << endl;
#define rep(i,m,n) for(ll i = (m); i < (n); i++)

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n, k;
    cin >> n >> k;
    int pmin = n;
    int pmax = 0;
    int order_count = 0;
    int order_checker = 0;
    vector<int> p(n);
    int unchange_count = 0;
    for (int i = 0; i < n; i++) {
        cin >> p.at(i);
    }
    for (int i = 0; i < k; i++) {
        pmin = min(p.at(i), pmin);
        if (i > 0 && p.at(i-1) < p.at(i)) {
            order_count++;
        }
        else {
            order_count = 1;
        }
        pmax = max(p.at(i), pmax);
        if (order_count == k) {
            order_checker++;
        }
    }
    // cout << order_checker << endl;
    for (int i = k; i < n; i++) {
        bool unchange = true;
        if (p.at(i-k) == pmin) {
            // cout << "boom" << endl;
            pmin = n;
            for (int j = i-k+1; j < i; j++) {
                pmin = min(p.at(j), pmin);
                // cout << "aa" << pmin << endl;
            }
        }
        else {
            unchange = false;
        }
        if (p.at(i-k) == pmax) {
            pmax = 0;
            for (int j = i-k+1; j < i; j++) {
                pmax = max(p.at(j), pmax);
            }
        }
        pmin = min(p.at(i), pmin);
        if (p.at(i-1) < p.at(i)) {
            order_count = min(k, order_count+1);
        }
        else {
            order_count = 1;
            unchange = false;
        }
        pmax = max(p.at(i), pmax);
        if (order_count == k) {
            order_checker++;
        }
        if (unchange) {
            unchange_count++;
        }
        // cout << "order_count=" << order_count << endl;
        // cout << "order_checker=" << order_checker << endl;
        // cout << "unchange_count=" << unchange_count << endl;
        // cout << "pmax=" << pmax << endl;
        // cout << "pmin=" << pmin << endl;
    }
    if (order_checker == 0) {
        cout << n - k + 1 - unchange_count << endl;
    }
    else if (order_checker == 1) {
        cout << n - k + 1 - unchange_count << endl;        
    }
    else {
        cout << n - k + 3 - order_checker - unchange_count << endl;
    }
}