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;

}

相关推荐
(●—●)橘子……8 小时前
力扣第503场周赛练习理解
python·学习·算法·leetcode·职场和发展·周赛
明志数科9 小时前
4D时序标注技术详解:让机器人理解连续动作的数据基础
java·算法·机器人
feng_you_ying_li9 小时前
C++复习二,继承与多态
c++
小小de风呀10 小时前
de风——【从零开始学C++】(十一):list的基本使用和模拟实现
开发语言·c++·list
KaMeidebaby10 小时前
卡梅德生物技术快报|原核表达系统工艺优化:包涵体重折叠 + 分子筛纯化实现功能 RBD 高效制备,附全参数配置
前端·人工智能·算法·数据挖掘·数据分析
三行数学10 小时前
Matlab之父克利夫·莫勒尔逝世
开发语言·matlab
陌路2010 小时前
C++高级进阶--夯实进阶基础(1)
开发语言·c++
无限码力10 小时前
携程0510笔试真题【单数组交换】
算法·携程笔试·携程笔试真题·携程0510笔试真题
梦想三三10 小时前
【PYthon词频统计与文本向量化】苏宁易购评论分析实战
开发语言·python
AI人工智能+电脑小能手10 小时前
【大白话说Java面试题 第93题】【Mysql篇】第23题:从查找速度来看,聚集索引和非聚集索引哪个更快?
java·开发语言·数据库·mysql·面试