1 条题解

  • 0
    @ 2024-7-25 13:30:04

    参考答案:

    #include<iostream>
    #include<deque>
    
    using namespace std;
    
    const int N = 1000010;
    int a[N];
    int n, k;
    
    int main()
    {
        deque<int >q;
        cin >> n >> k;
    
        for (int i = 1; i <= n; ++i) cin >> a[i];
        for (int i = 1; i <= n; ++i)
        {
            while (q.size() && q.back() > a[i]) q.pop_back();
            q.push_back(a[i]);
            if (i - k >= 1 && q.front() == a[i - k]) q.pop_front();
            if (i >= k) cout << q.front() << ' ';
        }
    
        q.clear();
        cout << endl;
    
        for (int i = 1; i <= n; ++i)
        {
            while (q.size() && q.back() < a[i]) q.pop_back();
            q.push_back(a[i]);
            if (i - k >= 1 && q.front() == a[i - k]) q.pop_front();
            if (i >= k) cout << q.front() << ' ';
        }
    
        return 0;
    }
    
    • 1

    信息

    ID
    75
    时间
    1000ms
    内存
    256MiB
    难度
    10
    标签
    递交数
    5
    已通过
    2
    上传者