蒟蒻求救,#2 WA

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

InoriILU @ 2022-11-16 22:04:08

#include <iostream>
#include <deque>
//#include <climits>

//const int LENGTH{ 13 };
//const int INTERVAL{ 5 };

int main()
{
    using namespace std;
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    //cout << (INT_MAX < 10e6) << endl << (LLONG_MAX < ) << endl;
    int n{}, k{};
    cin >> n >> k;
    long long* storage{ new long long[n] };
    for (int i = 0; i < n; i++) cin >> storage[i];

    deque<int> num;
    for (int i = 0; i < n; i++)
    {
        if (!num.empty() && i - num.front() >= k)
            num.pop_front();
        while (!num.empty() && storage[num.back()] > storage[i])
            num.pop_back();
        num.push_back(i);
        if (i >= k - 1)
            cout << storage[num.front()] << " ";
    }
    cout << endl;
    for (int i = 0; i < n; i++)
    {
        if (!num.empty() && i - num.front() >= k)
            num.pop_front();
        while (!num.empty() && storage[num.back()] < storage[i])
            num.pop_back();
        num.push_back(i);
        if (i >= k - 1)
            cout << storage[num.front()] << " ";
    }
    cout << endl;
}

用序号存储的单调队列,也没发现有啥问题呀()


by YukinoYukinoshita @ 2022-11-16 22:27:23

num没清空


by InoriILU @ 2022-11-16 22:36:24

@kid_magic 谢谢老哥qwq


|