当前位置:网站首页>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 :
边栏推荐
- Under what circumstances can the company dismiss employees
- Developing NES game (cc65) 03 and VRAM buffer with C language
- 快速读入
- 公司在什么情况下可以开除员工
- 1331. Array sequence number conversion: simple simulation question
- Not optimistic about Apple making AR, Luo Yonghao: I'll do it myself
- 【Base】优化性能到底在优化啥?
- 区块反转(暑假每日一题 7)
- Merge table rows - three levels of for loop traversal data
- Sub database and sub table may not be suitable for your system. Let's talk about how to choose sub database and sub table and newsql
猜你喜欢

The openatom openharmony sub forum was successfully held, and ecological and industrial development entered a new journey

leetcode:704二分查找

Yan Ji lost Beijing again, and more than half of the stores in the country were closed

新零售电商O2O模式解析

VS1003 debugging routine

Sub database and sub table may not be suitable for your system. Let's talk about how to choose sub database and sub table and newsql

Did kafaka lose the message

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

How does musk lay off staff?

DART 三维辐射传输模型申请及下载
随机推荐
Hongjiu fruit passed the hearing: five month operating profit of 900million Ali and China agricultural reclamation are shareholders
SuperMap iclient3d for webgl to realize floating thermal map
HC-05蓝牙模块调试从模式和主模式经历
力扣315计算右侧小于当前元素的个数
Initialization examples of several modes of mma8452q
上位机和三菱FN2x通信实例
JSP自定义标签之自定义分页标签02
Multiple items on a computer share a public-private key pair to pull the Gerrit server code
05 pyechars 基本图表(示例代码+效果图)
输入字符串,内有数字和非字符数组,例如A123x456将其中连续的数字作为一个整数,依次存放到一个数组中,如123放到a[0],456放到a[1],并输出a这些数
VS1003调试例程
牛客网二叉树题解
CCF201912-2 回收站选址
1331. 数组序号转换 : 简单模拟题
C for循环内定义int i变量出现的重定义问题
STM32F103 several special pins are used as ordinary io. Precautions and data loss of backup register 1,2
C# 泛型是什么、泛型缓存、泛型约束
Yan Ji lost Beijing again, and more than half of the stores in the country were closed
Markdown concise grammar manual
Minimally invasive electrophysiology has passed the registration: a listed enterprise with annual revenue of 190million minimally invasive mass production