TLE两个点的神奇单调队列

P1440 求m区间内的最小值

_coastline_ @ 2023-08-09 15:38:54

#include<iostream>
#include<queue>
using namespace std;
#define For(i , j , k) for(int i = j;i <= k;i++)
#define MaxN 2000005

int N , M;
int num[MaxN];

deque <int> q;

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0) , cout.tie(0);
    cin >> N >> M;
    For(i , 1 , N)
    {
        cin >> num[i];
        if(i > M)
        {
            while(!q.empty() && q.front() < i-M) q.pop_front();
            cout << num[ q.front() ] << endl;
        }
        else cout << (i == 1? 0 : num[ q.front() ]) << endl;

        while(!q.empty() && num[ q.back() ] > num[i]) q.pop_back();
        q.push_back(i);
    }
    return 0;
}

复杂度应该没有问题,但就是TLE了,求大佬指点qwq


by lisida0820 @ 2023-08-09 16:01:20

@xujiabin194 应该是读入太慢了,加个快读快写就行了


by _coastline_ @ 2023-08-09 16:05:10

@lisida0820 还是不行啊达哥QAQ


by lisida0820 @ 2023-08-09 16:07:34

@xujiabin194 草,为什么我试到可以。你再把deque改为数组试试,STL常数大


by _coastline_ @ 2023-08-09 16:22:47

@lisida0820 感谢达哥,机房江大佬帮我A了Orz


|