队列 队列(Queue) 是一种常见的线性数据结构,具有先进先出(FIFO, First In First Out)的特点。它类似于现实生活中的排队场景:最先加入队列的元素最先被处理。
用数组模拟队列 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 #include <iostream> using namespace std;const int N = 100010 ;int q[N], hh = 0 , tt = -1 ;void push (int x) { q[++tt] = x; } void pop () { hh++; } void empty () { if (tt >= hh) cout << "NO" << endl; else cout << "YES" << endl; } void query () { cout << q[hh] << endl; } int main () { int M; cin >> M; while (M--) { string op; cin >> op; if (op == "push" ) { int x; cin >> x; push (x); } else if (op == "pop" ) { pop (); } else if (op == "empty" ) { empty (); } else { query (); } } return 0 ; }
单调队列 左程云版 1 2 3 4 5 6 7 8 9 10 11 12 13 void solve () { }
acwing版 https://www.acwing.com/problem/content/156/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 #include <iostream> using namespace std;const int N = 1000010 ;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 = 0 ; i < n; i++) cin >> a[i]; int hh = 0 , tt = -1 ; for (int i = 0 ; i < n; i++) { if (hh <= tt && i - k + 1 > q[hh]) hh++; while (hh <= tt && a[i] <= a[q[tt]]) tt--; q[++tt] = i; if (i - k + 1 >= 0 ) cout << a[q[hh]] << " " ; } cout << endl; hh = 0 , tt = -1 ; for (int i = 0 ; i < n; i++) { if (hh <= tt && i - k + 1 > q[hh]) hh++; while (hh <= tt && a[i] >= a[q[tt]]) tt--; q[++tt] = i; if (i - k + 1 >= 0 ) cout << a[q[hh]] << " " ; } return 0 ; }
习题 滑动窗口的最大值 https://leetcode.cn/problems/sliding-window-maximum/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 class Solution {public : vector<int > maxSlidingWindow (vector<int >& arr, int k) { int n = arr.size (); vector<int > ans; deque<int > dq; for (int i = 0 ; i < k; i++) { while (!dq.empty () && arr[i] >= arr[dq.back ()]) dq.pop_back (); dq.push_back (i); } ans.push_back (arr[dq.front ()]); for (int r = k; r < n; r++) { int l = r - k + 1 ; while (!dq.empty () && dq.front () < l) dq.pop_front (); while (!dq.empty () && arr[r] >= arr[dq.back ()]) dq.pop_back (); dq.push_back (r); ans.push_back (arr[dq.front ()]); } return ans; } };
绝对差不超过限制的最长连续子数组 https://leetcode.cn/problems/longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 class Solution {public : int longestSubarray (vector<int >& arr, int limit) { deque<int > maxDeque; deque<int > minDeque; int ans = 0 ; int n = arr.size (); for (int r = 0 , l = 0 ; r < n; r++) { while (!maxDeque.empty () && arr[r] >= arr[maxDeque.back ()]) { maxDeque.pop_back (); } maxDeque.push_back (r); while (!minDeque.empty () && arr[r] <= arr[minDeque.back ()]) { minDeque.pop_back (); } minDeque.push_back (r); while (!maxDeque.empty () && !minDeque.empty () && arr[maxDeque.front ()] - arr[minDeque.front ()] > limit) { if (maxDeque.front () == l) maxDeque.pop_front (); if (minDeque.front () == l) minDeque.pop_front (); l++; } ans = max (ans, r - l + 1 ); } return ans; } };