小白学算法(11)单调栈与队列

182 阅读2分钟

开启掘金成长之旅!这是我参与「掘金日新计划 · 12 月更文挑战」的第15天,点击查看活动详情

单调栈

单调栈字面意思就可得是存储单调数字的栈,而这里我们还是采取静态数组模拟栈,由下图也可知,使用静态数组模拟一些数据结构,速度要快一点

上图STL stack,下列数组模拟

image-20221210191746758

1.模板介绍

int tt = 0;
for (int i = 1; i <= n; i ++ )
{
    //用一个while循环 寻找比插入数字小/大的数
    while (tt && check(stk[tt], i)) tt -- ;
    //遍历结束后将他插入栈
    //常见模型:找出每个数左边离它最近的比它大/小的数。上方遍历就丢掉一些数字,所以适合这种题
    stk[ ++ tt] = i;
}

2.实操演练

830. 单调栈 - AcWing题库

给定一个长度为 N 的整数数列,输出每个数左边第一个比它小的数,如果不存在则输出 −1−1。

#include<iostream>
using namespace std;
const int N=1e5+10;
int stk[N];
int tt=0;
int main()
{
    int n;
    cin>>n;
    while(n--)
    {
        int x;
        scanf("%d",&x);
        while(tt&&stk[tt]>=x)tt--;
        if(!tt)cout<<"-1"<<" ";
        else cout<<stk[tt]<<" ";
        stk[++tt]=x;
    }
    return 0;
}

STL stack解决

#include<iostream>
#include<stack>
using namespace std;
const int N=1e5+10;
stack<int>stk;
int tt=0;
int main()
{
    int n;
    cin>>n;
    while(n--)
    {
        int x;
        scanf("%d",&x);
        while(!stk.empty()&&stk.top()>=x)stk.pop();
        if(stk.empty())cout<<"-1"<<" ";
        else cout<<stk.top()<<" ";
        stk.push(x);
    }
    return 0;
}

可以使用 std::ios::sync_with_stdio(false);进行加快cin的输入

但使用这个语句时

就要做好只用 cin 读入和输出的准备

单调队列

1.模板介绍

常见模型:找出滑动窗口中的最大值/最小值
int hh = 0, tt = -1;
for (int i = 0; i < n; i ++ )
{
   // 先判断队头是否滑出窗口
    while (hh <= tt && check_out(q[hh])) hh ++ ;  
    //然后再进行插入数字的比较
    while (hh <= tt && check(q[tt], i)) tt -- ;
    q[ ++ tt] = i;
}

2.实操演练

154. 滑动窗口 - AcWing题库

给定一个大小为 n≤1e6的数组。

有一个大小为 k 的滑动窗口,它从数组的最左边移动到最右边。

你只能在窗口中看到 k 个数字。

每次滑动窗口向右移动一个位置。

以下是一个例子:

该数组为 [1 3 -1 -3 5 3 6 7],k 为 3。

窗口位置最小值最大值
[1 3 -1] -3 5 3 6 7-13
1 [3 -1 -3] 5 3 6 7-33
1 3 [-1 -3 5] 3 6 7-35
1 3 -1 [-3 5 3] 6 7-35
1 3 -1 -3 [5 3 6] 736
1 3 -1 -3 5 [3 6 7]37

你的任务是确定滑动窗口位于每个位置时,窗口中的最大值和最小值。

#include<iostream>
using namespace std;
const int N = 1e6+10;
//que构建队列,储存位置
//nums 构架队列中的元素
int que[N],nums[N],n,m;
int main()
{
    int n,m;
    cin>>n>>m;
    int hh=0,tt=-1;
    for(int i=0;i<n;i++)
    {
        scanf("%d",&nums[i]);
    }
    for(int i=0;i<n;i++)
    {
        //先判断此时队列头部是否超过限制3
        //超过就向前移
        if(i-m+1>que[hh])hh++;
        //hh<=tt 判断窗口是否存在
        //因为是单调队列 所以插入元素与队尾比较,小于 队尾就往前移
        while(hh<=tt&&nums[i]<=nums[que[tt]])tt--;
        que[++tt]=i;
        //判断窗口是否存在,存在最小值就是对头
        if(i-m+1>=0)printf("%d ",nums[que[hh]]);
    }
        printf("\n");
    //求最大值 只需要改变大小于号即可,别忘了重新初始化变量
        hh=0,tt=-1;
        for(int i=0;i<n;i++)
    {
        if(i-m+1>que[hh])hh++;
        while(hh<=tt&&nums[i]>=nums[que[tt]])tt--;
        que[++tt]=i;
        if(i-m+1>=0)printf("%d ",nums[que[hh]]);
    }
}

下面是使用STL容器解决

#include<iostream>
#include<deque>
const int N=1e6+10;
int nums[N];
using namespace std;
int main()
{
    int n , k;
    cin>>n>>k;
    //下标从1 开始避免第一批没有计入,因为是0,1,2 2-3=-1
    for(int i=1;i<=n;i++)
    {
        cin>>nums[i];
    }
    deque<int>que;
    for(int i=1;i<=n;i++)
    {
        if(i-k>=0&&que.front()==nums[i-k])
        {
            que.pop_front();
        }
        while(que.size()&&nums[i]<que.back())
        {
            que.pop_back();
        }
        que.push_back(nums[i]);
        if(i-k>=0)cout<<que.front()<<" ";
    }
    cout<<endl;
    que.clear();
        for(int i=1;i<=n;i++)
    {
        if(i-k>=0&&que.front()==nums[i-k])
        {
            que.pop_front();
        }
        while(que.size()&&nums[i]>que.back())
        {
            que.pop_back();
        }
        que.push_back(nums[i]);
        if(i-k>=0)cout<<que.front()<<" ";
    }
}