只能A一个点

P1886 滑动窗口 /【模板】单调队列

yu__xuan @ 2019-07-31 15:44:42

用的手写双端队列(不知道对不对)

#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<algorithm>
#define MAXN 1000001
#define rr register
using namespace std;
int n,k,a[MAXN],head=1,tail;
struct QwQ{
    int w,num;
}dq[MAXN];
inline void push_back(int x){dq[++tail].w=x;}
inline bool pop_front(){if(head<=tail)head++;return head<=tail;}
inline bool pop_back(){if(head<=tail)tail--;return head<=tail;}
inline int back(){return dq[tail].w;}
inline int front(){return dq[head].w;}
inline bool empty(){return head<=tail;}

inline int read(){
    int x=0;bool f=0;char c=getchar();
    while(c<'0'||c>'9'){if(c=='-')f=!f;c=getchar();}
    while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
    return f?-x:x;
}

int main(){
    freopen("testdata (1).in","r",stdin);
    freopen("qwq.out","w",stdout);
    n=read(),k=read();
    for(rr int i=1;i<=n;++i){
        a[i]=read();
    }
    dq[0].w=-0x7fffffff;
    for(rr int i=1;i<=n;++i){
        if(a[i]>back()){
            push_back(a[i]);
            dq[tail].num=i;
        }
        else{
            while(a[i]<back()){
                if(pop_back()==0) break;
            }
            push_back(a[i]);
            dq[tail].num=i;
        }
        if(i>3&&dq[tail].num-dq[head].num>=3) pop_front();
        if(i>=k) printf("%d ",front());
    }
    puts("");
    head=1,tail=0;
    dq[0].w=0x7fffffff;
    for(rr int i=1;i<=n;++i){
        if(a[i]<back()){
            push_back(a[i]);
            dq[tail].num=i;
        }
        else{
            while(a[i]>back()){
                if(pop_back()==0) break;
            }
            push_back(a[i]);
            dq[tail].num=i;
        }
        if(i>3&&dq[tail].num-dq[head].num>=3) pop_front();
        if(i>=k) printf("%d ",front());
    }
    return 0;
}

by yu__xuan @ 2019-07-31 15:48:09

请自行忽略freopen


by ctq1999 @ 2019-07-31 15:55:28

这应该单调栈做会更好


by ctq1999 @ 2019-07-31 15:55:32

@yu__xuan


by yu__xuan @ 2019-07-31 15:56:17

@ctq1999 这题不是单调队列吗?


by Aliemo @ 2019-07-31 15:56:18

你最小值错了

4 1 1 2 3 4 1 2 3 4

cout 1 2 3 4 1 2 3 4

你的是 1 1 1 2 1 2 3 4


by yu__xuan @ 2019-07-31 15:56:44

@情谊。。碎情 我还不知道我错了?关键是错哪儿,qwq


by ctq1999 @ 2019-07-31 15:57:31

哦说错了


by K2sen @ 2019-07-31 15:58:21

你可以用线段树水过去(逃


by yu__xuan @ 2019-07-31 15:58:24

@ctq1999 单调队列就是用双端队列来实现的吧,qwq


by yu__xuan @ 2019-07-31 16:01:14

A了,感谢@情谊。。碎情 大佬


|