滑动窗口

#include <bits/stdc++.h>
using namespace std;
#define IO ios::sync_with_stdio(false);cin.tie(0); cout.tie(0)
inline int lowbit(int x) { return x & (-x); }
#define ll long long
#define pb push_back
#define PII pair<int, int>
#define fi first
#define se second
#define inf 0x3f3f3f3f
const int N = 1e6 + 7;
int q[N];
int a[N];

int main() {
    IO;
    int n, m;
    cin >> n >> m;
    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 - m) ++hh;
        while (hh <= tt && a[q[tt]] >= a[i]) --tt;
        q[++tt] = i;
        if (i >= m)  cout << a[q[hh]] << " ";
    }
    cout << '\n';
    hh = 0, tt = -1;
    for (int i = 1; i <= n; ++i) {
        if (hh <= tt && q[hh] <= i - m) ++hh;
        while (hh <= tt && a[q[tt]] <= a[i]) --tt;
        q[++tt] = i;
        if (i >= m)  cout << a[q[hh]] << " ";
    }
    return 0;
}

posted @ 2021-02-04 19:41  phr2000  阅读(49)  评论(0编辑  收藏  举报