当前位置:网站首页>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 :
边栏推荐
- SuperMap game engine license module division
- Brief discussion on open source OS distribution
- Why do enterprises need the ability of enterprise knowledge management?
- 设计一个线程池
- The 'name' attribute value associated with the element type 'item' cannot contain '& lt;' Character solution
- Zadig v1.13.0 believes in the power of openness, and workflow connects all values
- C# 结构使用
- 机器学习实战-神经网络-21
- 20220728-Object类常用方法
- Come to tdengine Developer Conference and have an insight into the future trend of data technology development
猜你喜欢

行业落地呈现新进展 | 2022 开放原子全球开源峰会 OpenAtom OpenHarmony 分论坛圆满召开

GMT installation and use

SuperMap iclient3d for webgl to realize floating thermal map

Open source office (ospo) unveils Secrets

How does musk lay off staff?

洪九果品通过聆讯:5个月经营利润9亿 阿里与中国农垦是股东

试用copilot过程中问题解决

The usage and Simulation Implementation of vector in STL

Uninstall Navicat: genuine MySQL official client, really fragrant!

开源社区三十年 | 2022 开放原子全球开源峰会开源社区三十年专题活动圆满召开
随机推荐
输入字符串,内有数字和非字符数组,例如A123x456将其中连续的数字作为一个整数,依次存放到一个数组中,如123放到a[0],456放到a[1],并输出a这些数
Brief discussion on open source OS distribution
leetcode:数组
Redis实现分布式锁
设计一个线程池
SuperMap itablet license module division
Developing NES game (cc65) 03 and VRAM buffer with C language
Solution to the binary tree problem of niuke.com
Zadig v1.13.0 believes in the power of openness, and workflow connects all values
20220728-Object类常用方法
Sliding Window
01 pyechars 特性、版本、安装介绍
Come to tdengine Developer Conference and have an insight into the future trend of data technology development
03 pyechars 直角坐标系图表(示例代码+效果图)
stm32 回环结构接收串口数据并处理
恋爱男女十禁
C# 结构使用
04 pyechars 地理图表(示例代码+效果图)
Uncover why devaxpress WinForms, an interface control, discards the popular maskbox property
Unity加载Glb模型