Problem:
MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

Sliding Minimum Element

For a given array $a_1, a_2, a_3, ... , a_N$ of $N$ elements and an integer $L$, find the minimum of each possible sub-arrays with size $L$ and print them from the beginning. For example, for an array $\{1, 7, 7, 4, 8, 1, 6\}$ and $L = 3$, the possible sub-arrays with size $L = 3$ includes $\{1, 7, 7\}$, $\{7, 7, 4\}$, $\{7, 4, 8\}$, $\{4, 8, 1\}$, $\{8, 1, 6\}$ and the minimum of each sub-array is 1, 4, 4, 1, 1 respectively.

Constraints

$1 \leq N \leq 10^6$

$1 \leq L \leq 10^6$

$1 \leq a_i \leq 10^9$

$L \leq N$

Input

The input is given in the following format.

$N$ $L$

$a_1$ $a_2$ ... $a_N$

Output

Print a sequence of the minimum in a line. Print a space character between adjacent elements.

Sample Input 1

7 3
1 7 7 4 8 1 6

Sample Output 1

1 4 4 1 1

Solution in C# (judge verdict: Accepted)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using static System.Console;
using static System.Math;

class Program
{
    static void Main()
    {
        var N = FastIO.Int();
        var L = FastIO.Int();
        var A = new int[N];
        for (int i = 0; i < A.Length; i++)
        {
            A[i] = FastIO.Int();
        }
        WriteLine(string.Join(" ", SlideMin(A, L)));
    }

    public static int[] SlideMin(int[] a, int k)
    {
        var q = new Deque<int>();
        var ret = new int[a.Length - k + 1];
        for (int i = 0; i < a.Length; i++)
        {
            while (q.Count != 0 && a[q.PeekBack()] >= a[i])//>=ではなく>でもいいけど>=のほうが省メモリ
            {
                q.PopBack();
            }
            q.PushBack(i);
            var p = i - k + 1;
            if (p >= 0)
            {
                ret[p] = a[q.PeekFront()];
                if (p == q.PeekFront())
                {
                    q.PopFront();
                }
            }
        }
        return ret;
    }
}

public class Deque<T>
{
    T[] buffer;
    int front;
    int back;//半開区間
    int mask;//1ずつしか変化しないからMaskいらねとおもったけど-1とlength-1のとき分岐するよりMaskのほうが速いのかな

    public Deque() : this(1024)
    {

    }

    public Deque(int capacity)
    {
        var c = 1024;
        while (c < capacity) c <<= 1;//! 1 << (log2(x - 1) + 1)
        buffer = new T[c];
        mask = c - 1;
    }

    public int Count//プロパティはILで見ると結構命令数あって嫌だ
    {
        get
        {
            int count = back - front;
            if (count < 0) count += buffer.Length;
            return count;
        }
    }

    public void Extend()
    {
        var nb = new T[buffer.Length * 2];
        for (int i = 0; i < buffer.Length - 1; i++)
        {
            nb[i] = buffer[(front + i) & mask];
        }
        front = 0;
        back = buffer.Length - 1;
        buffer = nb;
        mask = mask << 1 | 1;
    }

    public void PushFront(T item)
    {
        if (Count == buffer.Length - 1) Extend();
        front = (front - 1) & mask;
        buffer[front] = item;
    }

    public void PushBack(T item)
    {
        if (Count == buffer.Length - 1) Extend();
        var e = back;
        back = (back + 1) & mask;
        buffer[e] = item;
    }

    public T PopFront()
    {
        var f = front;
        front = (front + 1) & mask;
        return buffer[f & mask];
    }

    public T PopBack()
    {
        back = (back - 1) & mask;
        return buffer[back];
    }

    public T PeekFront()
    {
        return buffer[front];
    }

    public T PeekBack()
    {
        return buffer[(back - 1) & mask];
    }

    public void Clear()
    {
        front = 0;
        back = 0;
        //if (default(T) == null) Array.Clear(buffer, 0, buffer.Length);
    }
}

public static class FastIO
{
    static System.IO.Stream str = System.Console.OpenStandardInput();
    const int size = 1024;
    static byte[] buffer = new byte[size];
    static int ptr = size;

    static byte Read()
    {
        if (ptr == size)
        {
            str.Read(buffer, 0, size);
            ptr = 0;
        }
        return buffer[ptr++];
    }

    public static int Int()
    {
        var c = Read();
        while (c < 0x21)
        {
            c = Read();
        }
        var n = false;
        if (c == '-')
        {
            n = true;
            c = Read();
        }
        var ret = 0;
        while (c > 0x20)
        {
            ret = ret * 10 + c - '0';
            c = Read();
        }
        return n ? -ret : ret;
    }
}