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.Linq;
using System.Numerics;
using System.Collections.Generic;
using System.IO;
using System.Text;

public class Solver
{
    public void Solve()
    {
        int n = ri, k = ri;
        List<int> p = new List<int>();
        for(int i = 0; i < n; i++) { p.Add(ri); }
        PriorityQueue<int> s = new PriorityQueue<int>(0);
        PriorityQueue<int> t = new PriorityQueue<int>(1);
        PriorityQueue<int> deletes = new PriorityQueue<int>(0);
        PriorityQueue<int> deletet = new PriorityQueue<int>(1);
        for (int i = 1; i <= k; i++)
        {
            s.Push(p[i]);
            t.Push(p[i]);
        }
        int q = 1;
        for (int i = 1; i < n - k; i++)
        {
            if (s.Peek()!=p[i-1]||t.Peek()!=p[i+k])
            {
                q++;
            }
            s.Push(p[i + k]);
            t.Push(p[i + k]);
            deletes.Push(p[i]);
            deletet.Push(p[i+k-1]);
            while (s.Peek() == deletes.Peek()) { s.Pop(); deletes.Pop(); }
            while (t.Peek() == deletet.Peek()) { t.Pop(); deletet.Pop(); }
        }
        Write(q);
    }

    int ri { get { return sc.Integer(); } }
    long rl { get { return sc.Long(); } }
    double rd { get { return sc.Double(); } }
    string rs { get { return sc.Scan(); } }
    public StreamScanner sc = new StreamScanner(Console.OpenStandardInput());
    public Algorithm alg = new Algorithm();

    private const long mod = 1000000007;
    long Reverse(long n) { return Pow(n, mod - 2); }
    int Sign(int n) { return n == 0 ? 0 : n > 0 ? 1 : -1; }
    int Abs(int n) { return (n > 0) ? n : -n; }
    int Gcd(params int[] n) { int p = n[0]; for (int i = 1; i < n.Length; i++) { p = Gcd(p, n[i]); } return p; }
    int Gcd(int a, int b) { return (a == 0) ? b : Gcd(b % a, a); }
    int Lcm(params int[] n) { int p = n[0]; for (int i = 1; i < n.Length; i++) { p = Lcm(p, n[i]); } return p; }
    int Lcm(int a, int b) { return a * b / Gcd(a, b); }
    int Max(params int[] n) { int p = n[0]; for (int i = 1; i < n.Length; i++) { p = (p > n[i]) ? p : n[i]; } return p; }
    int Min(params int[] n) { int p = n[0]; for (int i = 1; i < n.Length; i++) { p = (p < n[i]) ? p : n[i]; } return p; }
    long Mul(params long[] n) { long p = n[0]; for (long i = 1; i < n.Length; i++) { p = (p * n[i]) % mod; } return p; }
    long Pow(long n, long p) { long t = n, q = 1; while (p > 0) { if (p % 2 == 1) { q = (q * t) % mod; } t = (t * t) % mod; p >>= 1; } return q; }
    long PowUpper(long n, long b) { long p = 1; while (p <= n) { p *= b; } return p; }
    long Sign(long n) { return n == 0 ? 0 : n > 0 ? 1 : -1; }
    long Abs(long n) { return (n > 0) ? n : -n; }
    long Gcd(params long[] n) { long p = n[0]; for (long i = 1; i < n.Length; i++) { p = Gcd(p, n[i]); } return p; }
    long Gcd(long a, long b) { return (a == 0) ? b : Gcd(b % a, a); }
    long Lcm(params long[] n) { long p = n[0]; for (long i = 1; i < n.Length; i++) { p = Lcm(p, n[i]); } return p; }
    long Lcm(long a, long b) { return a * b / Gcd(a, b); }
    long Max(params long[] n) { long p = n[0]; for (long i = 1; i < n.Length; i++) { p = (p > n[i]) ? p : n[i]; } return p; }
    long Min(params long[] n) { long p = n[0]; for (long i = 1; i < n.Length; i++) { p = (p < n[i]) ? p : n[i]; } return p; }
    void Write<T>(T t) { Console.WriteLine(t.ToString()); }
    void YN(bool t) { Console.WriteLine(t ? "YES" : "NO"); }
    void Yn(bool t) { Console.WriteLine(t ? "Yes" : "No"); }
    void yn(bool t) { Console.WriteLine(t ? "yes" : "no"); }
}

static class Program
{
    static public void Main(string[] args)
    {
        Console.SetOut(new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false });
        var solver = new Solver();
        solver.Solve();
        Console.Out.Flush();
    }
}

public class Algorithm
{
    public int[] ZAlgorithm(string s)
    {
        int[] p = new int[s.Length];
        p[0] = s.Length;
        int i = 1, j = 0;
        while (i < s.Length)
        {
            while (i + j < s.Length && s[j] == s[i + j]) ++j;
            p[i] = j;
            if (j == 0) { ++i; continue; }
            int k = 1;
            while (i + k < s.Length && k + p[k] < j)
            {
                p[i + k] = p[k];
                ++k;
            }
            i += k; j -= k;
        }
        return p;
    }
}

public class PriorityQueue<T> where T : IComparable
{
    private IComparer<T> _comparer = null;
    private int _type = 0;

    private T[] _heap;
    private int _sz = 0;

    private int _count = 0;

    /// <summary>
    /// Priority Queue with custom comparer
    /// </summary>
    public PriorityQueue(IComparer<T> comparer)
    {
        _heap = new T[128];
        _comparer = comparer;
    }

    /// <summary>
    /// Priority queue
    /// </summary>
    /// <param name="type">0: asc, 1:desc</param>
    public PriorityQueue(int type = 0)
    {
        _heap = new T[128];
        _type = type;
    }

    private int Compare(T x, T y)
    {
        if (_comparer != null) return _comparer.Compare(x, y);
        return _type == 0 ? x.CompareTo(y) : y.CompareTo(x);
    }

    public void Push(T x)
    {
        _count++;
        if (_count > _heap.Length)
        {
            var newheap = new T[_heap.Length * 2];
            for (int n = 0; n < _heap.Length; n++) newheap[n] = _heap[n];
            _heap = newheap;
        }

        //node number
        var i = _sz++;

        while (i > 0)
        {
            //parent node number
            var p = (i - 1) / 2;

            if (Compare(_heap[p], x) <= 0) break;

            _heap[i] = _heap[p];
            i = p;
        }

        _heap[i] = x;
    }

    public T Pop()
    {
        _count--;

        T ret = _heap[0];
        T x = _heap[--_sz];

        int i = 0;
        while (i * 2 + 1 < _sz)
        {
            //children
            int a = i * 2 + 1;
            int b = i * 2 + 2;

            if (b < _sz && Compare(_heap[b], _heap[a]) < 0) a = b;

            if (Compare(_heap[a], x) >= 0) break;

            _heap[i] = _heap[a];
            i = a;
        }

        _heap[i] = x;

        return ret;
    }

    public int Count()
    {
        return _count;
    }

    public T Peek()
    {
        return _heap[0];
    }

    public bool Contains(T x)
    {
        for (int i = 0; i < _sz; i++) if (x.Equals(_heap[i])) return true;
        return false;
    }

    public void Clear()
    {
        while (this.Count() > 0) this.Pop();
    }

    public IEnumerator<T> GetEnumerator()
    {
        var ret = new List<T>();

        while (this.Count() > 0)
        {
            ret.Add(this.Pop());
        }

        foreach (var r in ret)
        {
            this.Push(r);
            yield return r;
        }
    }

    public T[] ToArray()
    {
        T[] array = new T[_sz];
        int i = 0;

        foreach (var r in this)
        {
            array[i++] = r;
        }

        return array;
    }
}

public class StreamScanner
{
    public StreamScanner(Stream stream) { str = stream; }
    private readonly Stream str;
    private readonly byte[] buf = new byte[1024];
    private int len, ptr;
    public bool isEof = false;
    public bool IsEndOfStream { get { return isEof; } }
    private byte read()
    {
        if (isEof) throw new EndOfStreamException();
        if (ptr >= len)
        {
            ptr = 0;
            if ((len = str.Read(buf, 0, 1024)) <= 0)
            {
                isEof = true;
                return 0;
            }
        }
        return buf[ptr++];
    }
    public char Char()
    {
        byte b = 0;
        do b = read();
        while (b < 33 || 126 < b);
        return (char)b;
    }
    public string Scan()
    {
        var sb = new StringBuilder();
        for (var b = Char(); b >= 33 && b <= 126; b = (char)read())
            sb.Append(b);
        return sb.ToString();
    }
    public long Long()
    {
        long ret = 0; byte b = 0; var ng = false;
        do b = read();
        while (b != '-' && (b < '0' || '9' < b));
        if (b == '-') { ng = true; b = read(); }
        for (; true; b = read())
        {
            if (b < '0' || '9' < b)
                return ng ? -ret : ret;
            else ret = ret * 10 + b - '0';
        }
    }
    public int Integer() { return (int)Long(); }
    public double Double() { return double.Parse(Scan()); }
}