#include <iostream>
#include <vector>
#include <set>
#include <deque>
#include <algorithm>
using namespace std;
void output(vector<int>& arr) {
int n = arr.size(), len = 0;
for (int i = 0; i < n; i++) {
len += printf("%3d", i);
}
cout << endl;
for (int i = 0; i < len; i++) cout << "-";
cout << endl;
for (int i = 0; i < n; i++) {
printf("%3d", arr[i]);
}
cout << endl;
return;
}
//单调队列,维护区间最值
int main() {
//n代表序列长度,k代表维护窗口长度
int n, k;
cin >> n >> k;
vector<int> arr;
//用双端队列,因为有尾部弹出元素操作
deque<int> q;
for (int i = 0, a; i < n; i++) {
cin >> a;
arr.push_back(a);
}
output(arr);
for (int i = 0; i < n; i++) {
//单调队列中存储下标,因为下标能找到值,值找不到下标
while (!q.empty() && arr[q.back()] > arr[i]) q.pop_back();
q.push_back(i); //存下标
if (i - q.front() == k) q.pop_front();
printf("[%d, %d] = arr[%d] = %d\n", max(i - k + 1, 0), i, q.front(), arr[q.front()]);
}
return 0;
}
10-28
998
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
11-08
818
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)