蒟蒻刚学OI,求助大佬

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

π酱 @ 2018-10-04 17:25:15

为什么这题用Deque(吸氧)过了,手写队列却Wa了2个点

QwQ

码风奇特,勿喷

手写队列代码:

#include<bits/stdc++.h>
using namespace std;
struct Node{
    int Data,Address;
}Maxn[1000010],Minn[1000010];
Node x,k;
int n,m,Front_Max,Back_Max,Front_Min,Back_Min,Ans_Max[1000100],Ans_Min[1000100];
template <typename T>
int Read(T &x) {
    x=0;
    int f=1;
    char c=getchar();
    for(; !isdigit(c); c=getchar()) if(c=='-') f=-f;
    for(; isdigit(c); c=getchar()) x=x*10+c-'0';
    x*=f;
    if(c=='\n')return 1;
    else return 0;
}
void Write(long long x)
{
    if(x<0){
        putchar('-');
        x=-x;
    } 
    if(x>9){
        Write((x-x%10)/10);
     }
    putchar(x%10+'0');
}
int main(){
    //freopen("Windows.in","r",stdin);
    //freopen("Windows.out","w",stdout);
    Read(n);
    Read(m);
    Front_Max=Back_Max=Front_Min=Front_Min=1;
    Maxn[1].Data=-10000000;
    Maxn[1].Address=1;
    Minn[1].Data=10000000;
    Minn[1].Address=1;
    for(int i=1;i<m;i++){
        Read(x.Data);
        x.Address=i;
        while((Maxn[Front_Max].Data<=x.Data)&&(Front_Max<=Back_Max))
            Back_Max--;
        Maxn[++Back_Max]=x;
        while((Minn[Front_Min].Data>=x.Data)&&(Front_Min<=Back_Min))
            Back_Min--;
        Minn[++Back_Min]=x; 
    }
    for(int i=m;i<=n;i++){
        Read(x.Data);
        x.Address=i;
        while((Maxn[Back_Max].Data<=x.Data)&&(Front_Max<=Back_Max))
            Back_Max--;
        Maxn[++Back_Max]=x;
        while((Minn[Back_Min].Data>=x.Data)&&(Front_Min<=Back_Min))
            Back_Min--;
        Minn[++Back_Min]=x;
        while((Maxn[Front_Max].Address<=i-m)&&(Front_Max<=Back_Max))
            Front_Max++;
        while((Minn[Front_Min].Address<=i-m)&&(Front_Min<=Back_Min))
            Front_Min++;
        Ans_Max[i-m]=Maxn[Front_Max].Data;
        Ans_Min[i-m]=Minn[Front_Min].Data;      
    }
    for(int i=0;i<n-m;i++){
        Write(Ans_Min[i]);
        putchar(' ');
    }
    Write(Ans_Min[n-m]);
        putchar('\n');
    for(int i=0;i<n-m;i++){
        Write(Ans_Max[i]);
        putchar(' ');
    }
    Write(Ans_Max[n-m]);
        putchar('\n');  
    return 0;
}

Deque代码:

#include<bits/stdc++.h>
using namespace std;
struct Node{
    int Data,Address;
};
deque<Node> Maxn,Minn;
Node x,k;
int n,m,Address[1000010],Ans_Max[1000100],Ans_Min[1000100];
template <typename T>
int Read(T &x) {
    x=0;
    int f=1;
    char c=getchar();
    for(; !isdigit(c); c=getchar()) if(c=='-') f=-f;
    for(; isdigit(c); c=getchar()) x=x*10+c-'0';
    x*=f;
    if(c=='\n')return 1;
    else return 0;
}
void Write(long long x)
{
    if(x<0){
        putchar('-');
        x=-x;
    } 
    if(x>9){
        Write((x-x%10)/10);
     }
    putchar(x%10+'0');
}
int main(){
    Read(n);
    Read(m);
    x.Data=-10000000;
    x.Address=0;
    Maxn.push_back(x);
    x.Data=10000000;
    Minn.push_back(x);
    for(int i=1;i<m;i++){
        Read(x.Data);
        x.Address=i;
        Node k;
        k=Maxn.back();
        while(x.Data>k.Data){
            Maxn.pop_back();
            if(Maxn.empty())
                break;
            k=Maxn.back();
        }
        Maxn.push_back(x);
        k=Minn.back();
        while(x.Data<k.Data){
            Minn.pop_back();
            if(Minn.empty())
                break;
            k=Minn.back();
        }
        Minn.push_back(x);
    }
    for(int i=m;i<=n;i++){
        Read(x.Data);
        x.Address=i;
        Node k;
        k=Maxn.back();
        while(x.Data>k.Data){
            Maxn.pop_back();
            if(Maxn.empty())
                break;
            k=Maxn.back();
        }
        if(!Maxn.empty()){          
            k=Maxn.front();
            while(k.Address<=i-m){
                Maxn.pop_front();
                if(Maxn.empty())
                    break;
                k=Maxn.front();
            }
        }
        Maxn.push_back(x);
        k=Minn.back();
        while(x.Data<k.Data){
            Minn.pop_back();
            if(Minn.empty())
                break;
            k=Minn.back();
        }
        if(!Minn.empty()){          
            k=Minn.front();
            while(k.Address<=i-m){
                Minn.pop_front();
                if(Minn.empty())
                    break;
                k=Minn.front();
            }
        }
        Minn.push_back(x);
        Ans_Max[i-m]=Maxn.front().Data;
        Ans_Min[i-m]=Minn.front().Data;
    }
    for(int i=0;i<n-m;i++){
        Write(Ans_Min[i]);
        putchar(' ');
    }
    Write(Ans_Min[n-m]);
        putchar('\n');
    for(int i=0;i<n-m;i++){
        Write(Ans_Max[i]);
        putchar(' ');
    }
    Write(Ans_Max[n-m]);
        putchar('\n');
    return 0;           
}

by caidzh @ 2018-10-04 17:27:46

码风真是奇特


by Viston @ 2018-10-04 17:33:08

我好像是暴力过的


by wwddd @ 2018-10-04 17:35:18

马蜂有我当年的感觉


by π酱 @ 2018-10-04 17:35:27

刚被机惨了,我不弱,我要AK IOI


by keydu @ 2018-10-04 17:36:37

。。。。这码风轻轻松松上百


by 雷人 @ 2018-10-04 17:37:48

Pascal+Java+C++超级大马蜂


by Utsuji_risshū @ 2018-10-04 17:40:15

@葛军 手写的话容易在头尾指针上写出问题啊,反正我当时只要写斜率优化头尾指针就出错


|