C++ 单调栈原理与模板

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

模板如下:

#include <iostream>

#include <stack>

using namespace std;

const int N = 3e6 + 10;

int a[N];

int n;

void test1()

{

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

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

{

//栈里面大于等于a[i] 的元素全部出栈

while(st.size() && st.top() >= a[i]) st.pop();

st.push(a[i]);

}

}

void text2()

{

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

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

{

//栈里面小于等于a[i] 的元素全部出栈

while(st.size() && st.top() <= a[i]) st.pop();

st.push(a[i]);

}

}

int main()

{

text1();

text2();

return 0;

}

单调栈解决的问问题

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

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

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

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

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

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

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

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

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

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

代码的实现:

单调递减的栈 左边

const int n = 1e5 + 10;

int n;

int a[N];

int ret[N];

void test()

{

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

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

{

while(st.size() && a[st.top()] <= a[i]) st.pop();

if(st.size()) ret[i] = st.top(); //下标

st.push(i);

}

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

cout << ret[i] << " ";

cout << endl;

}

int mian()

{

cin >> n;

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

{

cin >> a[i];

}

test();

return 0;

}

单调递增的栈 左边

#include <iostream>

#include <stack>

using namespace std;

const int n = 1e5 + 10;

int n;

int a[N];

int ret[N];

void test()

{

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

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

{

while(st.size() && a[st.top()] >= a[i]) st.pop();

if(st.size()) ret[i] = st.top(); //下标

st.push(i);

}

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

cout << ret[i] << " ";

cout << endl;

}

int mian()

{

cin >> n;

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

{

cin >> a[i];

}

test();

return 0;

}

右边 单调递减的栈

const int n = 1e5 + 10;

int n;

int a[N];

int ret[N];

void test()

{

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

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

{

while(st.size() && a[st.top()] <= a[i]) st.pop();

if(st.size()) ret[i] = st.top(); //下标

st.push(i);

}

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

cout << ret[i] << " ";

cout << endl;

}

int mian()

{

cin >> n;

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

{

cin >> a[i];

}

test();

return 0;

}

右边 单调增的栈

const int n = 1e5 + 10;

int n;

int a[N];

int ret[N];

void test()

{

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

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

{

while(st.size() && a[st.top()] >= a[i]) st.pop();

if(st.size()) ret[i] = st.top(); //下标

st.push(i);

}

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

cout << ret[i] << " ";

cout << endl;

}

int mian()

{

cin >> n;

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

{

cin >> a[i];

}

test();

return 0;

}

相关推荐
chao1898445 小时前
基于 SPEA2 的多目标优化算法 MATLAB 实现
开发语言·算法·matlab
沪漂阿龙5 小时前
AI大模型面试题:支持向量机是什么?间隔最大化、软间隔、核函数、LinearSVC 全面拆解
人工智能·算法·支持向量机
赏金术士5 小时前
Kotlin 习题集 · 高级篇
android·开发语言·kotlin
little~钰5 小时前
倍增算法和ST表
算法
楼兰公子6 小时前
buildroot 在编译rust时裁剪平台类型数量的方法
开发语言·后端·rust
知识领航员6 小时前
蘑兔AI音乐深度实测:功能拆解、实测表现与适用场景
java·c语言·c++·人工智能·python·算法·github
薛定e的猫咪6 小时前
因果推理研究方向综述笔记
人工智能·笔记·深度学习·算法
吴声子夜歌6 小时前
Go——并发编程
开发语言·后端·golang
ooseabiscuit7 小时前
Laravel4.x:现代PHP框架的奠基之作
java·开发语言·php