当前位置:网站首页>Monotonic stack
Monotonic stack
2022-07-28 12:46:00 【.DoubleBean.】
Luogu P5788 【 Templates 】 Monotonic stack
Title address
https://www.luogu.com.cn/problem/P5788
Title Description

Input
5
1 4 2 3 5
Output
2 5 4 5 0
Monotonic stack : Every time a new element is put on the stack , The elements in the stack keep orderly monotonic increasing or decreasing
The elements of the array can be compared to the height of an adult , Stand these people in a line , Start with one person , Ask the first person visible behind him
For example, in the example , Elements [4], Compare a height to 4 People who , Then the first person visible behind this person is [5], because [2],[3] Height ratio [4] Short , Is blocked. .
Then start creating monotone stacks :
Press data from back to front ,[5] Push , Then press in [3], also [2], Because it conforms to the principle of monotonous decline , that [3] The previous number , That is to say, bi [3] The taller one is [5], Than [2] The taller one is [3]. Press in [4] There is a slight change when , because [2],[3] All ratio [4] smaller ( Height ratio [4] Short ), So the [2],[3] All out of the stack , that [4] The previous number is [5], It also conforms to the principle of monotonic decline . And finally [2], Than the stack [4] smaller , Therefore, there is no operation of pressing out the stack , Is directly [4]
The code is as follows :
#include<iostream>
#include<cstdio>
#include<stack>
#define INF 3000005
using namespace std;
int line[INF], ans[INF];
//line Is an array that stores data ,ans Is an array of answers
int main(){
int i, n;
scanf("%d", &n);
for (i = 0; i < n; i++){
scanf("%d", &line[i]);
}
stack<int> s;
// Scan elements from back to front
for (i = n - 1; i >= 0; i--){
while (!s.empty() && line[s.top()] <= line[i]){
// Take two. " tall " Elements between elements are pressed out of the stack , Because they have no meaning to exist
s.pop();
}
ans[i] = s.empty() ? 0 : (s.top() + 1); // Get the location of the element
s.push(i); // Index , Not the elements
}
for (i = 0; i < n - 1; i++){
printf("%d ", ans[i]);
}
printf("%d", ans[i]);
return 0;
}
leetcode 503. Next bigger element II
Title address
https://leetcode-cn.com/problems/next-greater-element-ii
Title Description
Given a circular array ( The next element of the last element is the first element of the array ), Output the next larger element of each element
Numbers x The next larger element of is in array traversal order , The first larger number after this number ,
This means that you should cycle through it to the next larger number . If it doesn't exist , The output -1.
Example 1:
Input : [1,2,1]
Output : [2,-1,2]
explain : first 1 The next larger number of is 2;
Numbers 2 Can't find the next larger number ;
the second 1 The next largest number of need to loop search , The result is also 2.
Be careful : The length of the input array will not exceed 10000.
Ring array , But computer memory is linear , So we usually use linear array to simulate the effect of circular array , Yes % Operator remainder
The idea of this problem can be : Put the original array “ Double ”, It's followed by an original array , Then there is the template of monotone stack .
Here is the picture above , It should be easier to understand with the code .
Code
class Solution {
public:
vector<int> nextGreaterElements(vector<int>& nums) {
int i, len = nums.size();
stack<int> s;
vector<int> res(len);
// It can be seen as doubling the length of the array
for(i=2*len-1;i>=0;i--){
while(!s.empty() && s.top() <= nums[i%len]) // Remainder
s.pop();
res[i%len] = s.empty() ? -1 : s.top();
s.push(nums[i%len]);
}
return res;
}
};
The topic of monotonous stack :
边栏推荐
- Zadig v1.13.0 believes in the power of openness, and workflow connects all values
- leetcode 1518. 换酒问题
- 微创电生理通过注册:年营收1.9亿 微创批量生产上市企业
- Is it overtime to be on duty? Take up legal weapons to protect your legitimate rights and interests. It's time to rectify the working environment
- Siemens docking Leuze BPS_ 304i notes
- How does musk lay off staff?
- AVL树(平衡搜索树)
- LeetCode 移除元素&移动零
- 合并表格行---三层for循环遍历数据
- Use json.stringify() to format data
猜你喜欢

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

Distributed session solution

C for循环内定义int i变量出现的重定义问题

开源汇智创未来 | 2022 开放原子全球开源峰会 OpenAtom openEuler 分论坛圆满召开

Jinshanyun rushes to the dual main listing of Hong Kong stocks: the annual revenue of 9billion is a project supported by Lei Jun

MarkDown简明语法手册

leetcode:数组

OpenAtom OpenHarmony分论坛圆满举办,生态与产业发展迈向新征程

HC-05蓝牙模块调试从模式和主模式经历

【萌新解题】爬楼梯
随机推荐
DART 三维辐射传输模型申请及下载
力扣315计算右侧小于当前元素的个数
Developing NES games (cc65) 05 and palette with C language
Under what circumstances can the company dismiss employees
MarkDown简明语法手册
Brief discussion on open source OS distribution
Siemens docking Leuze BPS_ 304i notes
连通块&&食物链——(并查集小结)
【一知半解】零值拷贝
苏黎世联邦理工学院 | 具有可变形注意Transformer 的基于参考的图像超分辨率(ECCV2022))
Deployment之滚动更新策略。
Is it overtime to be on duty? Take up legal weapons to protect your legitimate rights and interests. It's time to rectify the working environment
FutureWarning: Indexing with multiple keys (implicitly converted to a tuple of keys) will be depreca
Unity加载Glb模型
AVL tree (balanced search tree)
leetcode 1518. 换酒问题
Zadig v1.13.0 believes in the power of openness, and workflow connects all values
leetcode:704二分查找
Sliding Window
SuperMap itablet license module division