TLE

P1440 求m区间内的最小值

xfx2011 @ 2024-03-10 15:04:36

#include<bits/stdc++.h>
using namespace std;
int n,m;
const int maxn = 2e6 + 10;
int a[maxn];
int res[maxn];
deque<int>q;
int main(){
    freopen("求m区间内的最小值.in","r",stdin);
    freopen("求m区间内的最值.out","w",stdout);
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> m;
    for(int i = 1;i <= n;i ++)
        cin >> a[i];
    for(int i = 1;i <= n;i ++){
        while(!q.empty() && a[i] < a[q.back()])
            q.pop_back();
        q.push_back(i);
        if(i >= m){
            while(!q.empty() && q.front() <= i - m)
                q.pop_front();
        }
        res[i] = a[q.front()];

    }
    cout << 0 << "\n";
    for(int i = 1;i < n;i ++){
            cout << res[i];
        cout << endl;       
    }
    return 0;
}

80TLE


by zjpwdyf @ 2024-03-10 15:24:11

输入输出换成 scanf 和 printf


by zjpwdyf @ 2024-03-10 15:25:15

另外建议手写单调队列,因为 deque 常数巨大无比。


by xfx2011 @ 2024-03-18 12:58:15

谢谢


by imsbNR @ 2024-04-13 08:04:55

建议直接把deque换成list,其他操作不变


by lw393 @ 2024-05-06 19:54:45

endl费时用'\n'就好


by xfx2011 @ 2024-05-26 12:05:41

ok


|