C++ 单调栈原理与模板

单调栈就是具有单调性的栈,他依旧是一个栈结构,只不过里面的数据是递增或递减的(严格的增或减)

模板如下:

#include <iostream>

#include <stack>

using namespace std;

const int N = 3e6 + 10;

int aN;

int n;

void test1()

{

stack<int> st;// 维护一个单调递增的栈

for(int i = 1; i <= n; i++)

{

//栈里面大于等于ai 的元素全部出栈

while(st.size() && st.top() >= ai) st.pop();

st.push(ai);

}

}

void text2()

{

stack<int> st; // 维护一个单调递减的栈

for(int i = 1; j<= n; i++)

{

//栈里面小于等于ai 的元素全部出栈

while(st.size() && st.top() <= ai) st.pop();

st.push(ai);

}

}

int main()

{

text1();

text2();

return 0;

}

单调栈解决的问问题

寻找当前元素左侧,理它最近,并且比他大的元素在哪;

寻找当前元素左侧,理它最近,并且比他小的元素在哪;

寻找当前元素右侧,理它最近,并且比他大的元素在哪;

寻找当前元素右侧,理它最近,并且比他大的元素在哪;

寻找当前元素左侧,理它最近,并且比他大的元素在哪;

寻找当前元素左侧,理它最近,并且比他大的元素在哪;

从左往右遍历元素,构造一个单调递减的栈,插入当前我位置的元素

如果栈为空,则左侧不存在比当前元素大的元素;

如果栈为非空,则插入当前位置时的栈顶元素就是所要找的元素;

注意:因为我们要找的是最终结果,因此,栈里面存在每个元素的下标

代码的实现:

单调递减的栈 左边

const int n = 1e5 + 10;

int n;

int aN;

int retN;

void test()

{

stack<int> st; // 单调递减的栈

for(int i = 1; i <= n; i++)

{

while(st.size() && ast.top() <= ai) st.pop();

if(st.size()) reti = st.top(); //下标

st.push(i);

}

for(int i = 1; i <= n; i++)

cout << reti << " ";

cout << endl;

}

int mian()

{

cin >> n;

for(int i = 1; i <= n; i++)

{

cin >> ai;

}

test();

return 0;

}

单调递增的栈 左边

#include <iostream>

#include <stack>

using namespace std;

const int n = 1e5 + 10;

int n;

int aN;

int retN;

void test()

{

stack<int> st; // 单调递增的栈

for(int i = 1; i <= n; i++)

{

while(st.size() && ast.top() >= ai) st.pop();

if(st.size()) reti = st.top(); //下标

st.push(i);

}

for(int i = 1; i <= n; i++)

cout << reti << " ";

cout << endl;

}

int mian()

{

cin >> n;

for(int i = 1; i <= n; i++)

{

cin >> ai;

}

test();

return 0;

}

右边 单调递减的栈

const int n = 1e5 + 10;

int n;

int aN;

int retN;

void test()

{

stack<int> st; // 单调递减的栈

for(int i = 1; i <= n; i++)

{

while(st.size() && ast.top() <= ai) st.pop();

if(st.size()) reti = st.top(); //下标

st.push(i);

}

for(int i = n; i >= 1; i--)

cout << reti << " ";

cout << endl;

}

int mian()

{

cin >> n;

for(int i = 1; i <= n; i++)

{

cin >> ai;

}

test();

return 0;

}

右边 单调增的栈

const int n = 1e5 + 10;

int n;

int aN;

int retN;

void test()

{

stack<int> st; // 单调递增的栈

for(int i = 1; i <= n; i++)

{

while(st.size() && ast.top() >= ai) st.pop();

if(st.size()) reti = st.top(); //下标

st.push(i);

}

for(int i = 1; i <= n; i++)

cout << reti << " ";

cout << endl;

}

int mian()

{

cin >> n;

for(int i = n; i >= 1; i--)

{

cin >> ai;

}

test();

return 0;

}

相关推荐
weixin_4407841140 分钟前
【HandlerThread实现原理】
android·java·开发语言
天空'之城41 分钟前
C 语言工业级通用组件手写 24:互补滤波
c语言·开发语言·互补滤波·嵌入式算法·工业级组件
XR1234567881 小时前
工厂车间无线网络方案对比:AGV漫游哪家强?
开发语言·php
傻啦嘿哟1 小时前
某短视频平台视频爬虫实战:抓取推荐流视频信息,绕过反爬的3种技巧
开发语言·爬虫·python
現実君2 小时前
【硬件进阶】AD22上位替换JLEDA教程
开发语言·php
mubei-1232 小时前
SpringDAO的用法
java·开发语言·数据库
小程故事多_803 小时前
从A2C、TRPO、PPO到GRPO,强化学习策略梯度算法完整演进与大模型落地实战解析
人工智能·算法
2601_956121974 小时前
二分算法(知识点+题目)
c++·算法
格林威5 小时前
多相机微秒级对齐:硬件触发 vs PTP(IEEE 1588)方案实战对比
开发语言·人工智能·数码相机·机器学习·计算机视觉·视觉检测·机器视觉
初级代码游戏6 小时前
iOS开发 Swift 速记2:三种集合类型 Array Set Dictionary
开发语言·ios·swift