90分求助,第三个点WA

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

yingjianzhi @ 2022-10-27 21:15:02

#include<bits/stdc++.h>
using namespace std;
int n,m,i,a[1001000],ans1[1001000],ans2[1001000],t2,h1,h2,t1,t3,q1[1001000],q2[1001000];
int main(){
    scanf("%d%d",&n,&m);
    for (i=1;i<=n;i++) scanf("%d",&a[i]);
    h1=t1=h2=t2=1;
    q1[1]=q2[1]=1;
    if (m==1) ans1[1]=ans2[2]=a[1];
    for (i=2;i<=n;i++){
        while (h1<=t1&&a[i]<=a[q1[t1]]) --t1;
        q1[++t1]=i;
        while (h2<=t2&&a[i]>=a[q2[t2]]) --t2;
        q2[++t2]=i;
        while (h1<=t1&&q1[h1]+m<=i) ++h1;
        while (h2<=t2&&q2[h2]+m<=i) ++h2;
        ans1[i]=a[q1[h1]],ans2[i]=a[q2[h2]];
    }
    for (i=m;i<=n;i++) printf("%d ",ans1[i]);puts("");
    for (i=m;i<=n;i++) printf("%d ",ans2[i]);

}

by w18980077022 @ 2022-10-27 21:20:21

@yingjianzhi 开long long


by yingjianzhi @ 2022-10-27 21:27:17

还是错,应该不是这个问题。


by FstAutoMaton @ 2022-11-03 18:29:00

while (h1<=t1&&a[i]<=a[q1[t1]]) --t1
while (h2<=t2&&a[i]>=a[q2[t2]]) --t2;
while (h1<=t1&&q1[h1]+m<=i) ++h1;
while (h2<=t2&&q2[h2]+m<=i) ++h2;

的等于号都去掉试试


by FstAutoMaton @ 2022-11-03 18:29:22

@yingjianzhi


by lu1no @ 2023-04-06 16:26:00

我也和你错的一样,好像窗口长度为1的时候会忽略掉第一个数


|