首页 > 其他 > 详细

滑动窗口

时间:2021-02-04 20:56:59      阅读:38      评论:0      收藏:0      [点我收藏+]
#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;
}

滑动窗口

原文:https://www.cnblogs.com/phr2000/p/14374622.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!