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;

}

相关推荐
持力行5 小时前
从C struct到C++中的class
c语言·c++
meilindehuzi_a6 小时前
从零理解并实现 RAG:用 LangChain.js 构建语义检索问答系统
开发语言·javascript·langchain
风流 少年6 小时前
Julia
开发语言·julia
江畔柳前堤7 小时前
GO01-Go 语言与主流编程语言深度对比
开发语言·人工智能·后端·微服务·云原生·golang·go
:-)7 小时前
算法-归并排序
java·开发语言·数据结构·算法·排序算法
GIS阵地8 小时前
QgsRasterDataProvider 完整详解(QGIS 3.40.13 C++)
开发语言·c++·qt·开源软件·qgis
yaoxin5211239 小时前
462. Java 反射 - 获取声明类与封闭类
java·开发语言·python
阿里嘎多学长9 小时前
2026-07-14 GitHub 热点项目精选
开发语言·程序员·github·代码托管
charlie11451419110 小时前
Cinux: 为大内核铺路
开发语言·c++·操作系统·现代c++
2501_9142459310 小时前
C语言设计模式详解:从理论到实践的完整指南
c语言·开发语言·设计模式