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# (Mono 4.6.2.0) (judge verdict: Runtime Error)

using System;
using System.Collections.Generic;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {

            string[] str = Console.ReadLine().Split(' ');//2つ以上のスペース区切り入力の取得
            int N, K;
            N = int.Parse(str[0]);
            K = int.Parse(str[1]);
            string[] lineStr = Console.ReadLine().Split(' ');//2つ以上のスペース区切り入力の取得
            int[] line = new int[N];
            for (int i = 0; i < N; ++i)
            {
                line[i] = int.Parse(lineStr[i]);
            }
            int[] tempLine = new int[N];
            for (int i = 0; i < N; ++i)
            {
                tempLine[i] = line[i];
            }
            int count = 0;
            int[][] temp = new int[N - 2][];

            for (int i = 0; i < N - K + 1; ++i)
            {
                quickSort(line, i, i + K - 1);
                bool noExist = true;
                for (int j = 0; j < count; ++j)
                {
                    if (Judge(temp[j], line))
                    {
                        noExist = false;
                        break;
                    }
                }

                
                if (noExist)
                {
                    temp[count] = new int[N];
                    for (int j = 0; j < N; ++j)
                        temp[count][j] = line[j];
                    count++;
                }

                for (int j = 0; j < N; ++j)
                {
                    line[j] = tempLine[j];
                }
            }

            Console.WriteLine("{0}", count);//改行付き出力
        }

        static int GetPivot(int[] line, int i, int j)
        {
            int k = i + 1;
            while (k <= j && line[i] == line[k])
                k++;
            if (k > j)
                return -1;
            if (line[i] >= line[k])
                return i;
            return k;
        }

        static int DividePartition(int[] line, int i, int j, int x)
        {
            int l = i, r = j;
            while (l <= r)
            {
                while (l <= j && line[l] < x)
                    l++;
                while (r >= i && line[r] >= x)
                    r--;

                if (l > r) break;
                Swap(line, l, r);
                l++; r--;
            }
            return l;
        }

        static void quickSort(int[] line, int i, int j)
        {
            if (i == j) return;
            int p = GetPivot(line, i, j);
            if (p != -1)
            {
                int k = DividePartition(line, i, j, line[p]);
                quickSort(line, i, k - 1);
                quickSort(line, k, j);
            }
        }

        static void Swap(int[] line, int i, int j)
        {
            int tmp = line[i];
            line[i] = line[j];
            line[j] = tmp;
        }

        static bool Judge(int[] tempLine, int[] line)
        {
            for (int i = 0; i < line.Length; ++i)
            {
                if (line[i] != tempLine[i])
                {
                    return false;
                }
            }
            return true;
        }
    }
}