154. 滑动窗口
https://www.acwing.com/problem/content/156/
#include <iostream> #include <algorithm> #include <deque> using namespace std; const int N = 1000010; int n, k; int a[N]; // int q[N]; void runMin() { deque<int> q; // int hh = 0, tt = -1; for (int i = 0; i < n; i++) { if (!q.empty() && q.front() < i - k + 1) { q.pop_front(); } while (!q.empty() && a[q.back()] >= a[i]) { q.pop_back(); } q.push_back(i); if (i >= k - 1) { printf("%d ", a[q.front()]); } // if (hh <= tt && q[hh] <= i - k) hh ++ ; // while (hh <= tt && a[q[tt]] >= a[i]) tt -- ; // q[ ++ tt] = i; // if (i >= k - 1) printf("%d ", a[q[hh]]); } } void runMax() { deque<int> q; // int hh = 0, tt = -1; for (int i = 0; i < n; i++) { if (!q.empty() && q.front() < i - k + 1) { q.pop_front(); } while (!q.empty() && a[q.back()] <= a[i]) { q.pop_back(); } q.push_back(i); if (i >= k - 1) { printf("%d ", a[q.front()]); } // if (hh <= tt && q[hh] <= i - k) hh ++ ; // while (hh <= tt && a[q[tt]] <= a[i]) tt -- ; // q[ ++ tt] = i; // if (i >= k - 1) printf("%d ", a[q[hh]]); } } int main() { scanf("%d%d", &n, &k); for (int i = 0; i < n; i ++ ) scanf("%d", &a[i]); runMin(); puts(""); runMax(); puts(""); return 0; }