一、单调队列
1.单调队列引入
单调队列是指一个队列内部的元素具有严格单调性的一种数据结构,分为单调递增队列和单调递减队列。
单调队列满足两个性质:
(1)单调队列必须满足从队首到队尾的严格单调性;
(2)排在队列前面的比排在队列后面的要先进队列;单调队列问题又被称为”滑动窗口"问题,他是维持一个单调窗口,元素进队列是,将该元素与队尾元素进行对比,在维持的窗口为单调递增情况下,如果改元素大于队尾元素,那么该元素直接扔进队列,直到满足该元素大于队尾元素。
题目:给定一个长度为n的数组和一个大小为m的滑动窗口(0<m<=n)请找出所有华东窗口里的最大值。
例如,数组{2,6,4,9,8,5,5,2},滑动窗口的大小为3,那么共存在6个滑动窗口,他们的最大值分别为6,9,9,9,8,5
分析:6个滑动窗口及其最大值:{[2,6,4],9,8,5,5,2},6;{2,[6,4,9],8,5,5,2},9;{2,6,[4,9,8],5,5,2},9;{2,6,4,[9,8,5],5,2},9;{2,6,4,9,[8,5,5],2},8;{2,6,4,9,8,[5,5,2]},5;一、朴素做法:(计算窗口最大值需要O(m),移动n-m+1次,时间复杂度为O(nm)。
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int m = 3, n = 8;
// cin >> m >> n;
int a[] = {0,2,6,4,9,8,5,5,2};
// for (int i = 1; i <= n; i++)
// {
// cin >> a[i];
// }
for (int i = m; i <= n; i++)
{
int mx = a[i];
for (int j = i - m + 1; j <= i; j++)
{
mx = max(mx, a[j]);
}
cout << mx << " ";
}
2.单调队列实现
单调队列:队列内的元素是单调的,递增或者递减。用单调队列存储当前窗口的单调递减的元素,并且队头是窗口内的最大值,队尾是窗口内的尾元素。也就是说队列从队头到队尾对应窗口内从最大值到尾元素的一个子序列。
1.对头元素:当队头的元素从窗口滑出时,队头元素出队。
2.队尾入队:当新元素滑入窗口时,要把新元素从队尾插入,分两种情况:
(1)直接插入:如果新元素小于队尾元素,那就直接从队尾插入,因为它可能在前面的最大值滑出窗口时称为最大值;
(2)先删后插:如果新元素大于等于队尾元素,那就删除队尾元素(因为它不可能成为窗口中的最大值),循环删除直到队空或者遇到一个大于新元素的值,插入其后。这样每次能从队头取窗口最大值。
⭐⭐注意:队列应该存储窗口元素的下标值,便于判断队头出队。
数组模拟队列
#include<bits/stdc++.h>
using namespace std;
int main(){
int h = 0,t = -1;int n,m;
cin >> n >> m;
int a[n],q[n];//q[n]数组模拟队列,存储元素下标
for(int i = 1; i <= n; i++){
cin >> a[i];
}
for(int i = 1; i <= n; i++){
//q[h]不在窗口[i - m + 1,i]内,队头出队
if(h <= t && q[h] < i - m + 1){
h++;
}
//当前值≥队尾值,队尾出队
while(h <= t && a[i] >= a[q[t]]) t--;
//下标入队,便于队头出队
q[++t] = i;
//使用队头最大值
if(i > m - 1) cout << a[q[h]] <<" ";
}}
STL实现
#include<bits/stdc++.h>
#include<deque>
using namespace std;
deque<int> q;
int main(){
int n,m;
cin >> n >>m;
int a[n];
for(int i = 1; i <= n; i++){
cin >> a[i];
}
for(int i = 1; i <= n; i++){
//判断队头是否需要出队
if(!q.empty() && q.front() < i - m + 1){
q.pop_front();
}
//维护队列单调性
while(!q.empty() && a[i] > a[q.back()]){
q.pop_back();
}
//下标入队,便于队头出队
q.push_back(i);
//取队头元素作为窗口最大值
if(i > m - 1) cout << a[q.front()] <<" ";
}
}
例题:【模板】单调队列
题目描述 有一个长为 nn 的序列 aa,以及一个大小为 kk 的窗口。现在这个从左边开始向右滑动,每次滑动一个单位,求出每次滑动后窗口中的最大值和最小值。例如:
The array is [1,3,-1,-3,5,3,6,7][1,3,−1,−3,5,3,6,7], and k=3。
输入格式
输入一共有两行,第一行有两个正整数 n,kn,k。 第二行 nn 个整数,表示序列 aa
输出格式
输出共两行,第一行为每次窗口滑动的最小值
第二行为每次窗口滑动的最大值
输入输出样例
输入
8 3
1 3 -1 -3 5 3 6 7
输出
-1 -3 -3 -3 3 3
3 3 5 5 6 7
二、单调栈
1.定义:
单调递增栈:栈中元素从栈底到栈顶是递增的。
单调递减栈:栈中元素从栈底到栈顶是递减的。
2.应用:
求解下一个大于x元素或者是小于x元素的位置。
给一个数组,返回一个大小相同的数组,返回的数组的第i个位置的值应当是,对于原数组中的第i个元素,至少往右走多少步,才能遇到一个比自己大的元素(如果之后没有比自己大的元素,或者已经是最后一个元素,则在返回数组的位置放上-1)
#include <iostream>
#include <stack>
#include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std;
int num[100] = {1, 3, 4, 5, 2, 9, 6};
int res[100];
struct info
{
int no;
int data;
};
stack<info> s;
void nextGreater(int a[], int n)
{
for (int i = n - 1; i >= 0; i--)
{
while (!s.empty() && s.top().data <= a[i])
{
s.pop();
}
res[i] = s.empty() ? -1 : s.top().no - i;
info temp;
temp.no = i;
temp.data = num[i];
s.push(temp);
}
}
int main()
{
nextGreater(num, 7);
for (int i = 0; i < 7; i++)
{
cout << res[i] << " ";
}
}
现在求第一个比他大的元素值
#include<iostream>
#include<cmath>
#include<stack>
#include<algorithm>
using namespace std;
const int maxn = 1e6 + 10;
int res[maxn];
int num[maxn];
stack<int> s;
void nextGreater(int a[],int n){
for(int i = n; i >= 1; i --){
while(!s.empty() && a[i] >= s.top()){
s.pop();
}
res[i] = s.empty()?-1:s.top();
s.push(a[i]);
}
}
int main(){
int n;
cin >> n;
for(int i = 1; i <= n; i++){
cin >> num[i];
}
nextGreater(num,n);
for(int i = 1; i <= n; i++){
cout << res[i] <<" ";
}
}
求距离其多少步
#include <iostream>
#include <algorithm>
#include <cmath>
#include <stack>
using namespace std;
const int n = 1e6 + 10;
int res[n];
int num[n];
struct info
{
int no;
int data;
};
stack<info> s;
void nextGreaterid(int a[], int n)
{
for (int i = n; i >= 1; i--)
{
while (!s.empty() && a[i] >= s.top().data)
{
s.pop();
}
res[i] = s.empty() ? -1 : s.top().no - i;
info temp;
temp.no = i;
temp.data = num[i];
s.push(temp);
}
}
int main()
{
int n;
cin >> n;
for (int i = 1; i <= n; i++)
{
cin >> num[i];
}
nextGreaterid(num, n);
for (int i = 1; i <= n; i++)
{
cout << res[i] << " ";
}
}