90分,求调代码

P1440 求m区间内的最小值

yhr1111 @ 2024-07-16 19:38:20

#include<bits/stdc++.h>
using namespace std;
const int N=2e6+10;
deque<int>q;
int a[N];
//queue<int>q;
int main()
{
  int n,m;
   scanf("%d %d",&n,&m);
    for(int i=1;i<=n;i++)
        scanf("%d",&a[i]);
    for(int i=1;i<=n;i++)
    {
        if(i!=n)   
     while(!q.empty()&&a[q.back()]>a[i])q.pop_back();//

        while(!q.empty()&&q.front()<=i-m-1)q.pop_front();
//         cout<<a[q.front()]<<" ";
            if(i==1)
               printf("0\n");
           else
              printf("%d\n",a[q.front()]);
              q.push_back(i);
    }
    return 0;
}

by kaixi0928 @ 2024-07-23 18:10:46

#include<bits/stdc++.h>
using namespace std;
int n,m,a[2000004];
deque<int> q;
int main(){
    cin>>n>>m;
    for(int i=1;i<=n;i++) scanf("%d",&a[i]); 
    printf("0\n");
    for(int i=2;i<=n;i++){
        while(!q.empty()&&a[q.back()]>a[i-1]){
            q.pop_back();
        }
        q.push_back(i-1);
        while(!q.empty()&&q.front()<i-m){
            q.pop_front();
        }
        printf("%d\n",a[q.front()]);
    }
    return 0;
}

by yhr1111 @ 2024-07-24 09:08:37

@kaixi0928 谢谢兄弟,但是我不了解的是,我的代码什么地方出错误了,感觉没问题,但是我的代码在测试点中会报错,可以交我的代码试下,帮忙找错误(●'◡'●).


|