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 天前
算法竞赛C++常用的STL
c++·算法
weilx12343 天前
C++笔记-文件IO-<fcntl.h>
c++
小羊没烦恼!3 天前
初探性能优化——2个月到4小时的性能提升
java·开发语言·windows·算法·c#
伞伞悦读3 天前
【第38期】Python 模块与包详解:import、from、模块搜索路径、包结构和 __init__
开发语言·python
猎头南楼3 天前
知识社区推荐系统实践:新用户冷启动与长短期兴趣建模的挑战 资深推荐算法工程师
人工智能·深度学习·算法·机器学习
Smileyqp沛沛3 天前
前端?C++ ?较大差异基础罗列
c++·基础·前端转c++
C语言小火车3 天前
C/C++ 为什么需要编译器?
开发语言·c++
旖旎夜光3 天前
力控面试题 01.01: 判定字符是否唯一(位运算) —— 题解
c++·学习·算法·leetcode·力控
wzdark3 天前
大规模并行计算中的负载均衡算法研究4
算法
吞下星星的少年·-·3 天前
C++ 萌新语法入门篇
c++·算法比赛