Luo Gu sliding window 1886

First, I recommend one pair of very useful Bolg .

This problem minimum and maximum of two parts, essentially the same approach. Node structure is recorded in a value v, pos records the position, h team head pointer, a tail pointer team t, h, and t are each independently at two portions, so to initial values, respectively, which is monotone queue first question, the mouth is not likely paste.

 

#include<bits/stdc++.h>
using namespace std;
const int N=1e6+5;
int n,k,h,t,a[N];
struct node
{
    int v,pos;
}Q[N]; 
int main ()
{
    scanf("%d%d",&n,&k);
    for(int i=1;i<=n;i++)    scanf("%d",&a[i]);
    h=1;t=0;
    for(int i=1;i<=n;i++)
    {
        while(h<=t&&Q[h].pos+k<=i)    h++;
        while(h<=t&&Q[t].v>=a[i])    t--;
        Q[++t].v=a[i];    Q[t].pos=i;
        if(i>=k)    printf("%d ",Q[h].v);
    }
    h=1;t=0;
    puts("");
    for(int i=1;i<=n;i++)
    {
        while(h<=t&&Q[h].pos+k<=i)    h++;
        while(h<=t&&Q[t].v<=a[i])    t--;
        Q[++t].v=a[i];    Q[t].pos=i;
        if(i>=k)    printf("%d ",Q[h].v);
    }
    puts("");
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/Siv0106/p/11741927.html