一些算法

单调队列 滑动窗口

code

#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e6 + 100;
int a[N], q[N];

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

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

    return 0;
}

单调栈

posted @ 2025-05-03 16:32  闫柏军  阅读(15)  评论(0)    收藏  举报