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;

}

相关推荐
Dxy12393102167 小时前
Python 使用正则表达式将多个空格替换为一个空格
开发语言·python·正则表达式
故事和你918 小时前
洛谷-数据结构1-1-线性表1
开发语言·数据结构·c++·算法·leetcode·动态规划·图论
脱氧核糖核酸__8 小时前
LeetCode热题100——53.最大子数组和(题解+答案+要点)
数据结构·c++·算法·leetcode
脱氧核糖核酸__9 小时前
LeetCode 热题100——42.接雨水(题目+题解+答案)
数据结构·c++·算法·leetcode
techdashen9 小时前
Rust项目公开征测:Cargo 构建目录新布局方案
开发语言·后端·rust
星空椰9 小时前
JavaScript 进阶基础:函数、作用域与常用技巧总结
开发语言·前端·javascript
忒可君9 小时前
C# winform 自制分页功能
android·开发语言·c#
Rust研习社10 小时前
Rust 智能指针 Cell 与 RefCell 的内部可变性
开发语言·后端·rust
王老师青少年编程10 小时前
csp信奥赛C++高频考点专项训练之贪心算法 --【线性扫描贪心】:数列分段 Section I
c++·算法·编程·贪心·csp·信奥赛·线性扫描贪心
王老师青少年编程10 小时前
csp信奥赛C++高频考点专项训练之贪心算法 --【线性扫描贪心】:分糖果
c++·算法·贪心算法·csp·信奥赛·线性扫描贪心·分糖果