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;

}

相关推荐
破茧蝴蝶3 小时前
Python分布式爬虫完整教程
开发语言·python
狗凯之家源码网3 小时前
全新 PHP 轻量客服聊天室源码深度评测
开发语言·php·聊天室系统
远离UE44 小时前
UE5 Substrate Thin Film 源码拆析
开发语言·javascript·ue5
keyipatience4 小时前
JavaScript学习(1)书写位置,变量、数据类型、运算符
开发语言·前端·javascript·css·学习
hanlin034 小时前
刷题笔记:力扣第32题-最长有效括号
笔记·算法·leetcode
一拳一个呆瓜4 小时前
【STL】文件系统导航
c++·stl
All for pursuit.4 小时前
【动态规划-21】312.戳气球
数据结构·c++·算法·leetcode·动态规划
栈帧之外5 小时前
【C++】STL之set
开发语言·c++
catchadmin5 小时前
免费可商用 PHP 管理后台 CatchAdmin V5.4.1 发布,新增 MCP 支持
开发语言·php
zhanghaha13146 小时前
C++构造函数的一些其他应用(逐词逐句保姆讲解)
c++