当前位置:网站首页>单调栈Monotonic Stack
单调栈Monotonic Stack
2022-07-28 11:49:00 【.DoubleBean.】
洛谷 P5788 【模板】单调栈
题目地址
https://www.luogu.com.cn/problem/P5788
题目描述

Input
5
1 4 2 3 5
Output
2 5 4 5 0
单调栈:使得每次新元素入栈后,栈内的元素都保持有序的单调递增或单调递减
可以把数组的元素比喻成人的身高,将这些人站成一列,从一个人开始,求他后面可见的第一个人
例如样例中,元素[4],比喻一个身高为4的人,那么这个人的后面可见的第一个人就是[5],因为[2],[3]身高都比[4]矮,被挡住了。
之后便开始创建单调栈:
从后向前压入数据,[5]入栈,之后压入[3],还有[2],因为符合单调递减的原则,那么[3]前一个数,也就是比[3]身高要高的是[5],比[2]身高要高的是[3]。在压入[4]的时候稍微有些变化,因为[2],[3]都比[4]要小(身高比[4]矮),所以把[2],[3]都压出栈,那么[4]前一个数就是[5],同样符合单调递减的原则。最后是[2],比堆栈里的[4]要小,所以无压出栈的操作,直接就是[4]
代码如下:
#include<iostream>
#include<cstdio>
#include<stack>
#define INF 3000005
using namespace std;
int line[INF], ans[INF];
//line是存储数据的数组,ans是存储答案的数组
int main(){
int i, n;
scanf("%d", &n);
for (i = 0; i < n; i++){
scanf("%d", &line[i]);
}
stack<int> s;
//从后向前扫描元素
for (i = n - 1; i >= 0; i--){
while (!s.empty() && line[s.top()] <= line[i]){
//把两个"高个"元素之间的元素压出栈,因为它们没有存在的意义
s.pop();
}
ans[i] = s.empty() ? 0 : (s.top() + 1); //得到元素所在位置
s.push(i); //加入索引,而不是元素
}
for (i = 0; i < n - 1; i++){
printf("%d ", ans[i]);
}
printf("%d", ans[i]);
return 0;
}
leetcode 503. 下一个更大元素 II
题目地址
https://leetcode-cn.com/problems/next-greater-element-ii
题目描述
给定一个循环数组(最后一个元素的下一个元素是数组的第一个元素),输出每个元素的下一个更大元素
数字 x 的下一个更大的元素是按数组遍历顺序,这个数字之后的第一个比它更大的数,
这意味着你应该循环地搜索它的下一个更大的数。如果不存在,则输出 -1。
示例 1:
输入: [1,2,1]
输出: [2,-1,2]
解释: 第一个 1 的下一个更大的数是 2;
数字 2 找不到下一个更大的数;
第二个 1 的下一个最大的数需要循环搜索,结果也是 2。
注意: 输入数组的长度不会超过 10000。
环形数组,但计算机的内存都是线性的,所以一般都是用线性数组来模拟出环形数组的效果,用到了%运算符求余数
而这道题的思路可以是:将原始数组“翻倍”,就是在后面再接一个原始数组,之后便是套用单调栈的模板。
下面放上图,配合着代码一起看应该会更容易理解一些。
代码
class Solution {
public:
vector<int> nextGreaterElements(vector<int>& nums) {
int i, len = nums.size();
stack<int> s;
vector<int> res(len);
//可以看作是数组长度翻倍
for(i=2*len-1;i>=0;i--){
while(!s.empty() && s.top() <= nums[i%len]) //取余
s.pop();
res[i%len] = s.empty() ? -1 : s.top();
s.push(nums[i%len]);
}
return res;
}
};
单调栈的题目:
边栏推荐
- 利用依赖包直接实现分页、SQL语句
- What SaaS architecture design does a software architect need to know?
- 十三. 实战——常用依赖的作用
- Developing NES games with C language (cc65) 11. Metatiles
- HMS core audio editing service supports 7 kinds of audio effects to help one-stop audio processing
- Tik tok "founder" Yang Luyu, farewell byte?
- stm32 回环结构接收串口数据并处理
- Pits encountered in MSP430 development (to be continued)
- Multi Chain and multi currency wallet system development cross chain technology
- Deployment之滚动更新策略。
猜你喜欢

03 pyechars 直角坐标系图表(示例代码+效果图)

软件架构师必需要了解的 saas 架构设计?

Aopmai biological has passed the registration: the half year revenue is 147million, and Guoshou Chengda and Dachen are shareholders

First in the country! The two standards of "data product registration" formulated by insight technology and Shandong data were officially released

Ten prohibitions for men and women in love

图书馆自动预约脚本
![[dark horse morning post] LETV 400 employees have no 996 and no internal papers; Witness history! 1 euro =1 US dollar; Stop immediately when these two interfaces appear on wechat; The crackdown on cou](/img/d7/4671b5a74317a8f87ffd36be2b34e1.jpg)
[dark horse morning post] LETV 400 employees have no 996 and no internal papers; Witness history! 1 euro =1 US dollar; Stop immediately when these two interfaces appear on wechat; The crackdown on cou

Unity installs the device simulator
![[half understood] zero value copy](/img/5b/18082c1ea93f2e3bbf4920d73163fd.png)
[half understood] zero value copy

连通块&&食物链——(并查集小结)
随机推荐
FutureWarning: Indexing with multiple keys (implicitly converted to a tuple of keys) will be depreca
非标自动化设备企业如何借助ERP系统,做好产品质量管理?
How does musk lay off staff?
Ten prohibitions for men and women in love
力扣315计算右侧小于当前元素的个数
Baidu map API adds information window circularly. The window only opens at the last window position and the window information content is the same solution
金山云冲刺港股拟双重主要上市:年营收90亿 为雷军力挺项目
Multi Chain and multi currency wallet system development cross chain technology
Developing NES games with C language (cc65) 06. Sprites
Unity加载Glb模型
VS1003 debugging routine
区块反转(暑假每日一题 7)
Not optimistic about Apple making AR, Luo Yonghao: I'll do it myself
Using Arduino to develop esp8266 to build a development environment
C语言项目中使用json
Merge table rows - three levels of for loop traversal data
MarkDown简明语法手册
HC-05蓝牙模块调试从模式和主模式经历
Four authentic postures after suffering and trauma, Zizek
连通块&&食物链——(并查集小结)