0分代码求助……

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

liufukang @ 2020-12-30 21:17:19

#include<bits/stdc++.h>
using namespace std;
const int maxN=1e6+10;
deque<int>q;
int n,k,a[maxN];
int read();
void write(int),work(int);
int main(){
    n=read();k=read();
    for (int i=1;i<=n;i++) a[i]=read();//读入
    work(1);//最小值
    work(-1);//最大值
    return 0;
}
inline void work(int t){
    q.clear();//清空队列
    for (int i=1;i<=n;i++){
        while (!q.empty()&&q.front()<=i-k) q.pop_front();
        while (!q.empty()&&a[q.back()]*t>=a[i]*t) q.pop_back();
        q.push_back(i);
        if (i>=k) write(a[q.front()]),putchar(' ');
    }
    puts("");
}
inline int read(){
    int s=0,f=1;
    char ch=getchar();
    while (!isdigit(ch)){
        if (ch=='-') f=-1;
        ch=getchar();
    }
    while (isdigit(ch)){
        s=s*10+(ch-'0');
        ch=getchar();
    }
    return s*f;
}//快读
inline void write(int x){
    if (x<0){
        putchar('-');
        x=-x;
    }
    if (x>9){
        write(x/10);
        putchar(x%10+'0');
    }
    putchar(x+'0');
}//快写

在线等待大佬神助Orz


by Leap_Frog @ 2020-12-30 21:45:40

@刘福康 输出写挂了,换成printf就A了


by Leap_Frog @ 2020-12-30 21:46:03

我从来没见过这种错误(((


by WanderingTrader @ 2020-12-30 21:58:08

@刘福康 write函数改成这样试试

inline void write(int x){
    if (x<0){
        putchar('-');
        x=-x;
    }
    if (x>9){
        write(x/10);
        putchar(x%10+'0');
        return;
    }
    putchar(x+'0');
}//快写

by liufukang @ 2020-12-30 21:59:39

@Peal_Frog @wandering_trader 感谢大佬


|