当前位置:网站首页>单调栈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;
}
};
单调栈的题目:
边栏推荐
- Using dependent packages to directly implement paging and SQL statements
- 界面控件Telerik UI for WPF - 如何使用RadSpreadsheet记录或评论
- 新零售电商O2O模式解析
- MMA8452Q几种模式的初始化实例
- Science 重磅:AI设计蛋白质再获突破,可设计特定功能性蛋白质
- Siemens docking Leuze BPS_ 304i notes
- Why do enterprises need the ability of enterprise knowledge management?
- STM32F103 几个特殊引脚做普通io使用注意事项以及备份寄存器丢失数据问题1,2
- Implementation method of mouse hover, click and double click in ue4/5
- 03 pyechars 直角坐标系图表(示例代码+效果图)
猜你喜欢

Developing NES games with C language (cc65) 08. Background collision

Brief discussion on open source OS distribution

设计一个线程池

FutureWarning: Indexing with multiple keys (implicitly converted to a tuple of keys) will be depreca

scala 转换、过滤、分组、排序

Implementation method of mouse hover, click and double click in ue4/5

Analysis of new retail e-commerce o2o model

What SaaS architecture design does a software architect need to know?

Newly released, the domestic ide developed by Alibaba is completely open source

How does musk lay off staff?
随机推荐
Developing NES games (cc65) 05 and palette with C language
The usage and Simulation Implementation of vector in STL
05 pyechars 基本图表(示例代码+效果图)
Siemens docking Leuze BPS_ 304i notes
苏黎世联邦理工学院 | 具有可变形注意Transformer 的基于参考的图像超分辨率(ECCV2022))
Fastjson parses multi-level JSON strings
上位机和三菱FN2x通信实例
SuperMap arsurvey license module division
The input string contains an array of numbers and non characters, such as a123x456. Take the consecutive numbers as an integer, store them in an array in turn, such as 123 in a[0], 456 in a[1], and ou
开源社区三十年 | 2022 开放原子全球开源峰会开源社区三十年专题活动圆满召开
Pits encountered in MSP430 development (to be continued)
Hc-05 Bluetooth module debugging slave mode and master mode experience
C# 结构使用
Configure jupyter remote server
Kuzaobao: summary of Web3 encryption industry news on July 13
SuperMap iclient3d for webgl to realize floating thermal map
Open source office (ospo) unveils Secrets
输入字符串,内有数字和非字符数组,例如A123x456将其中连续的数字作为一个整数,依次存放到一个数组中,如123放到a[0],456放到a[1],并输出a这些数
Hongjiu fruit passed the hearing: five month operating profit of 900million Ali and China agricultural reclamation are shareholders
20220728-Object类常用方法