求助,快读为什么会让答案错误

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

ChengJY_ @ 2021-05-15 10:03:16

这是我的代码,在本地上没有问题,在洛谷上就会出问题,后来把快读删了就过了,有大佬知道原因吗

#include<bits/stdc++.h>
using namespace std;
int n,k,a[1000005],p[1000005],q[1000005];
inline int read(){
    char ch=getchar();
    int x=0,w=1;
    while((ch>'9' and ch<'0') and ch!='-') ch=getchar();
    if(ch=='-') w=-1,ch=getchar();
    while(ch>='0' and ch<='9')x=(x<<3)+(x<<1)+(ch^48),ch=getchar();
    return x*w;
}
void work_min(){
    int h=1,t=0;
    for(int i=1;i<=n;++i){
        while(h<=t and a[p[t]]>=a[i])
            t--;
        p[++t]=i;
        while(p[h]<=i-k)
            h++;
        if(i>=k)
            printf("%d ",a[p[h]]);
    } 
    printf("\n");
}
void work_max(){
    int h=1,t=0;
    for(int i=1;i<=n;++i){
        while(h<=t and a[q[t]]<=a[i])
            t--;
        q[++t]=i;
        while(q[h]<=i-k)
            h++;
        if(i>=k)
            printf("%d ",a[q[h]]);
    } 
    printf("\n");
}
int main(){
    n=read();k=read();
    for(int i=1;i<=n;++i)
        a[i]=read();
    work_min();
    work_max();
    return 0;
}

by Z_Healer @ 2021-05-15 10:04:55

你确定你的快读没写错?


by _caiji_ @ 2021-05-15 10:06:08

请求出一个字符 ch,它满足:

\begin{cases} ch>\texttt{'9'}\\ ch<\texttt{'0'} \end{cases}

by Plozia @ 2021-05-15 10:07:15

while((ch>'9' and ch<'0') and ch!='-') ch=getchar();

这句话写错了,应该是

while((ch>'9' or ch<'0') and ch!='-') ch=getchar();

by Z_Healer @ 2021-05-15 10:07:35

如果你不嫌弃的话这是我的

inline int read(){
    int w=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9'){
        if(ch=='-') f=-1;
        ch=getchar();
    }
    while(ch<='9'&&ch>='0'){
        w=(w<<3)+(w<<1)+(ch^48);
        ch=getchar();
    }
    return w*f;
}

by simonG @ 2021-05-15 10:15:22

il void read(int &x) {
    int s=0;x=1;
    char ch=getchar();
    while(ch<'0'||ch>'9') {if(ch=='-')x=-1;ch=getchar();}
    while(ch>='0'&&ch<='9') s=(s<<3)+(s<<1)+ch-'0',ch=getchar();
    x*=s;
}

传址调用更快


by ChengJY_ @ 2021-05-18 20:57:20

对不起,没想到是快读打错了,谢谢楼上的大佬们。


by Hoks @ 2022-08-12 20:54:38

@ChengJY_ 如果你不嫌弃可以用我老师给我的这个(函数一劳永逸的解决了刚才那个判定问题)

int read()
{
  char c=getchar();int x=0,f=1;
  while(!isdigit(c)){if(c=='-') f=-1;c=getchar();}
  while(isdigit(c)) x=(x<<1)+(x<<3)+(c^48),c=getchar();
  return x*f;
}

|