单调队列

单调队列

https://ac.nowcoder.com/acm/problem/50528

#include <cstdio>
using namespace std;
const int maxn= 1e6 + 4;

int a[maxn], dq[maxn];

int main()
{
    int n, k;
    scanf("%d %d", &n, &k);
    for(int i = 1; i <= n; ++i) scanf("%d", &a[i]);
    int l = 0, r = 1;
    dq[0] = 1;
    if(k == 1) printf("%d ", a[1]);
    for(int i = 2; i <= n; ++i)
    {
        if(i - dq[l] >= k && l < r) ++l;
        while(r > l && a[dq[r - 1]] >= a[i]) --r;
        dq[r++] = i;
        if(i >= k) printf("%d ", a[dq[l]]);
    }
    printf("\n");

    l = 0, r = 1;
    dq[0] = 1;
    if(k == 1) printf("%d ", a[1]);
    for(int i = 2; i <= n; ++i)
    {
        if(i - dq[l] >= k && l < r) ++l;
        while(r > l && a[dq[r - 1]] <= a[i]) --r;
        dq[r++] = i;
        if(i >= k) printf("%d ", a[dq[l]]);
    }
    printf("\n");
}

 

posted @ 2020-10-19 00:14  .Ivorelectra  阅读(56)  评论(0编辑  收藏  举报