落谷过了,POJ却TLE

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

xinruian @ 2023-10-20 21:24:53

代码如下,求大佬指点

#include <iostream>

using namespace std;

const int N = 1e6 + 5;

int a[N];
int q[N], hh, tt = -1;

int main() {
    ios::sync_with_stdio(0);
    int n, k;
    cin >> n >> k;
    for (int i = 1; i <= n; i ++)
        cin >> a[i];
    for (int i = 1; i <= n; i ++) {
        while (hh <= tt && q[hh] <= i - k)
            hh ++;
        while (hh <= tt && a[q[tt]] >= a[i]) 
            tt --;
        q[++ tt] = i;
        if (i >= k)
            cout <<a[q[hh]]  << ' ';
    }
    cout << endl;
    hh = 0, tt = -1;
    for (int i = 1; i <= n; i ++) {
        while (hh <= tt && q[hh] <= i - k)
            hh ++;
        while (hh <= tt && a[q[tt]] <= a[i]) 
            tt --;
        q[++ tt] = i;
        if (i >= k)
            cout << a[q[hh]] << ' ';
    }
    cout << endl;
    return 0;
}

by xinruian @ 2023-10-20 21:25:51

poj该题链接


by sinsop90 @ 2023-10-20 21:26:36

@xinruian 可能是读入太慢了


by SqRt_FiSh @ 2023-10-20 21:28:47

@xinruian 用read?


by forever_nope @ 2023-10-20 21:29:22

很正常,POJ 的老评测机。

卡卡问题不大。

另外,加上 cin.tie(0);


by xinruian @ 2023-10-21 16:33:33

@RainPPR 嗯嗯谢谢,加了就过了


|