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: Wrong Answer)

using System;
using System.Collections.Generic;
class Program
{
	static void Main(string[] args)
	{
		string[] str = Console.ReadLine().Split();
		int N = int.Parse(str[0]);
		int K = int.Parse(str[1]);
		string[] str2 = Console.ReadLine().Split();
		int[]C = new int[N];
		for(var i=0;i<N;i++){
			C[i] = int.Parse(str2[i]);
		}
		int ct = 0;
		int act = 0;
		for(var i=1;i<K;i++){
			if(C[i]<C[i-1]){
				ct++;
			}
		}
		if(ct==0){
			act++;
		}
		for(var i=K;i<N;i++){
			if(C[i]<C[i-1]){
				ct++;
			}
			if(C[i-K+1]<C[i-1-K+1]){
				ct--;
			}
			if(ct==0){
				act++;
			}
		}
		int ans = N-K+1;
		if(act>=1){
			ans -= act-1;
        }
		var pq = new PriorityQueue<int>((x,y)=>y-x);
		var pq2 = new PriorityQueue<int>();
		for(var i=0;i<K;i++){
			pq.Enqueue(C[i]);
			pq2.Enqueue(C[i]);
		}
		int act2 = 0;
      	int trc = K-1;
		int[]D = new int[N];
		for(var i=K;i<N;i++){
			while(true){
				if(D[pq2.heap[0]]==1){
                  	int a1 = pq2.Dequeue();
                } else {
                  	break;
                }
            }
			while(true){
				if(D[pq.heap[0]]==1){
					int a1 = pq.Dequeue();
                } else {
                  	break;
                }
            }
			//Console.Error.WriteLine(pq.heap[0]+" "+pq.heap[1]+" "+pq.heap[2]);
			//Console.Error.WriteLine(act2+" "+i+" "+trc+" "+D[0]+"trc:"+pq.heap[trc]+" "+C[i]+" "+C[i-K]);
			if(C[i]>pq.heap[0]){
				if(C[i-K]==pq2.heap[0]){
					act2++;
                }
            }
			pq.Enqueue(C[i]);
			pq2.Enqueue(C[i]);
          	trc++;
			D[C[i-K]] = 1;
		}
		ans -= act2;
		Console.WriteLine(ans);
	}
}

class PriorityQueue<T>
{
    public List<T> heap;
    private readonly Comparison<T> compare;
    public int size;
    public PriorityQueue() : this(Comparer<T>.Default) {} 
    public PriorityQueue(IComparer<T> comparer) : this(16, comparer.Compare) { }
    public PriorityQueue(Comparison<T> comparison) : this(16, comparison) { }
    public PriorityQueue(int capacity, Comparison<T> comparison)
    {
        this.heap = new List<T>(capacity);
        this.compare = comparison;
    }
    public void Enqueue(T item)
    {
        this.heap.Add(item);
        var i = size++;
        while (i > 0)
        {
            var p = (i - 1) >> 1;
            if (compare(this.heap[p], item) <= 0)
                break;
            this.heap[i] = heap[p];
            i = p;
        }
        this.heap[i] = item;

    }
    public T Dequeue()
    {
        var ret = this.heap[0];
        var x = this.heap[--size];
        var i = 0;
        while ((i << 1) + 1 < size)
        {
            var a = (i << 1) + 1;
            var b = (i << 1) + 2;
            if (b < size && compare(heap[b], heap[a]) < 0) a = b;
            if (compare(heap[a], x) >= 0)
                break;
            heap[i] = heap[a];
            i = a;
        }
        heap[i] = x;
        heap.RemoveAt(size);
        return ret;
    }
    public T Peek() { return heap[0]; }
    public int Count { get { return size; } }
    public bool Any() { return size > 0; }
}