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 Java8 (OpenJDK 1.8.0) (judge verdict: Time Limit Exceeded)

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;

public class Main {

    public static void main(String[] args) throws Exception {
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(System.in))) {
            String[] array = reader.readLine().split(" ");
            int N = Integer.parseInt(array[0]);
            int K = Integer.parseInt(array[1]);
            array = reader.readLine().split(" ");
            int[] P = new int[N];
            for (int i = 0; i < N; i++) {
                P[i] = Integer.parseInt(array[i]);
            }

            int kind = 0;
            int[] partial = new int[K];

            boolean same = false;

            for (int i = 0; i < N - K + 1; i++) {
                if (i == 0) {
                    System.arraycopy(P, i, partial, 0, K);
                    Arrays.sort(partial);
                } else {
                    if (P[i - 1] == partial[0]) {
                        if (P[i + K - 1] > partial[K - 1]) {
                            System.arraycopy(partial, 1, partial, 0, K - 1);
                            partial[K - 1] = P[i + K - 1];
                            continue;
                        }
                    }

                    int from = Arrays.binarySearch(partial, P[i - 1]);
                    int to = a(partial, P[i + K - 1], 0, K);
                    if (from < to) {
                        to--;
                    }

                    if (from < to) {
                        System.arraycopy(partial, from + 1, partial, from, to - from);
                    } else if (from > to) {
                        System.arraycopy(partial, to, partial, to + 1, from - to);
                    }
                    partial[to] = P[i + K - 1];
                }

                boolean z = true;
                for (int j = 0; j < K; j++) {
                    if (partial[j] != P[i + j]) {
                        z = false;
                        break;
                    }
                }
                if (z) {
                    if (same) {
                        continue;
                    }
                    same = true;
                }

                kind++;
            }

            System.out.println(kind);
        }
    }

    public static int a(int[] partial, int value, int from, int to) {
        if (to - from > 4) {
            if (partial[from + (to - from) / 2] > value) {
                return a(partial, value, from, from + (to - from) / 2);
            } else {
                return a(partial, value, from + (to - from) / 2, to);
            }
        }

        for (int j = from; j < to; j++) {
            if (partial[j] > value) {
                return j;
            }
        }
        return to;
    }

}