两个TLE,求正解

P1440 求m区间内的最小值

lhw0624 @ 2024-07-03 08:15:58

#include <bits/stdc++.h>
using namespace std;
const int N=1e6+11;
struct ST{
    int x,id;
};
ST q[N];
int a[N],b[N],c[N];
int head,tail;
int main(){
    int n,m,e=0;
    cin>>n>>m;
    head=tail=1;
    for(int i=1;i<n;i++){
        scanf("%d",&a[i]);
        if(head<tail && i-q[head].id>=m){
            head++;
        }
        while(head<tail && q[tail-1].x>a[i]){
            tail--;
        }
        q[tail].x=a[i];
        q[tail].id=i;
        tail++;
        e++;
        if(e==1) cout<<0<<endl;
        cout<<q[head].x<<endl;
    }
}

by linanchen @ 2024-07-03 08:30:47

1.数组开2e6
2.cout换成printf


by linanchen @ 2024-07-03 08:31:36

#include <bits/stdc++.h>
using namespace std;
const int N=2e6+11;
struct ST{
    int x,id;
};
ST q[N];
int a[N],b[N],c[N];
int head,tail;
int main(){
    int n,m,e=0;
    cin>>n>>m;
    head=tail=1;
    for(int i=1;i<n;i++){
        scanf("%d",&a[i]);
        if(head<tail && i-q[head].id>=m){
            head++;
        }
        while(head<tail && q[tail-1].x>a[i]){
            tail--;
        }
        q[tail].x=a[i];
        q[tail].id=i;
        tail++;
        e++;
        if(e==1) cout<<0<<endl;
        printf("%d\n",q[head].x);
    }
}

by lhw0624 @ 2024-07-03 09:05:57

@linanchen thank you !!!!!!


|