滑动窗口 单调队列模板
作者:
多米尼克領主的致意
,
2024-05-04 16:41:27
,
所有人可见
,
阅读 3
STL deque写法
#include<bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
deque<int>q;
int a[N];
int n, m;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
for(int i = 1;i <= n;i++)cin >> a[i];
for(int i = 1;i <= n;i++)
{
while(q.size() && a[i] < a[q.back()])q.pop_back();
q.push_back(i);
if(i >= m)
{
while(q.size() && i - q.front() >= m)q.pop_front();
cout << a[q.front()] << " ";
}
}
cout << endl;
deque<int>q;
for(int i = 1;i <= n;i++)
{
while(q.size() && a[i] > a[q.back()])q.pop_back();
q.push_back(i);
if(i >= m)
{
while(q.size() && i - q.front() >= m)q.pop_front();
cout << a[q.front()] << " ";
}
}
}