当前位置:网站首页>What is monotone stack
What is monotone stack
2022-07-24 06:12:00 【It's really all right, duck】
What is monotone stack
A monotonic stack is a stack that monotonically increases or decreases , That is, increasing or decreasing from the bottom of the stack to the top of the stack , According to this structure of monotone stack , It's easy to think that using monotone stack can easily O(n²) The time complexity is optimized to O(n), If you use arrays , The relative spatial complexity will not be too high
Example
Given a length of N The whole number sequence of , Output the first number smaller than it on the left of each number , If not, output −1.
Input format
The first line contains integers N, Represents the length of a sequence .
The second line contains N It's an integer , Represents an integer sequence .
Output format
All in one line , contain N It's an integer , Among them the first i The number represents the number i The first smaller number on the left of the number , If not, output −1.
Data range
1≤N≤1e5
1≤ The elements in the sequence ≤1e9sample input :
5 3 4 2 7 5sample output :
-1 3 -1 2 2
Ideas
The first thing to think about is violence , Define a stack , Then start from the top of the stack every time to find a number smaller than the current element , Find it and output it directly , No output found -1, Finally, put the current element on the stack . The time complexity of violent practices is O(n²), subject N But 1e5,O(n²) The time complexity of will obviously time out , So we think about whether there is something that can be optimized .
Let's see if there is any nature , See if there are some elements in this stack that will never be output as answers . Take a chestnut , For example, we ask for the subscript 8 The first smaller number on the left of the number of , Here we use array to simulate stack , Define a stack a, Subscript to be 8 The previous numbers have all been put on the stack , We assume that a3>=a5, that a3 Will it never be output as an answer , Why? a3 It won't be output as an answer forever ? because a5 stay a3 In front of , When we traverse the stack, we start from the top of the stack , The first traversal must be a5 instead of a3, Again because a3>=a5, therefore a3 Will never be output as an answer .
So we can find , If there is such a relationship in the stack ax>=ay And x<y Words ,ax It can be deleted , So the last sequence we have left must be a monotonic sequence , This is the monotone stack .
When the stack is monotonically increasing , At this time, a x, We need to find the first one in the stack x Small number . We start from the top of the stack ,x Wait, I want to enter the stack , If the stack top element is smaller than x Big words , because x To enter the stack , The top element of the stack will not be output , So the top of the stack should be deleted , Then traverse the stack in turn to find the ratio x Small numbers . If the stack top element is smaller than x Small words , Direct output , then x Into the stack .
Our code uses arrays to simulate stacks , Because it's faster and better .
The demo

Code implementation
#include <iostream>
using namespace std;
const int N=1e5+10;
int s[N];
int t;
int main()
{
int n;
cin>>n;
for(int i=0;i<n;i++)
{
int x;
cin>>x;
// If x Smaller than the top element , Delete stack top element , Traverse the stack until you find the ratio x Small numbers
while(t&&s[t]>=x) t--;
if(t) cout<<s[t]<<" ";// If not to the bottom of the stack
else cout<<"-1"<<" ";// If you get to the bottom of the stack
s[++t]=x;//x Into the stack
}
return 0;
}边栏推荐
- 头歌 平台作业
- Common methods of array
- HoloLens2开发:使用MRTK并且模拟眼动追踪
- Native JS magnifying glass effect
- Thymeleaf快速入门学习
- day-7 jvm完结
- Day-7 JVM end
- Dameng database_ Common user management commands
- JUC concurrent programming foundation (9) -- thread pool
- [USB host] stm32h7 cubemx porting USB host with FreeRTOS to read USB disk, usbh_ Process_ The OS is stuck. There is a value of 0xa5a5a5
猜你喜欢

Sequential stack C language stack entry and exit traversal

使用Keras实现 基于注意力机制(Attention)的 LSTM 时间序列预测

Foundation of JUC concurrent programming (8) -- read write lock

MySQL download and installation environment settings

顺序栈 C语言 进栈 出栈 遍历

HoloLens2开发:使用MRTK并且模拟眼动追踪

HoloLens 2开发:使用MRTK并在Unity中模拟手势输入

机器学习&深度学习 入门资料分享总结

Synergy LAN realizes multi host shared keyboard and mouse (AMD, arm)

MySQL基础---约束
随机推荐
day-7 jvm完结
day2-WebSocket+排序
Dameng database_ LENGTH_ IN_ Influence of char and charset
Positional argument after keyword argument
Foundation of JUC concurrent programming (1) -- related basic concepts
使用Qt连接MySql并创建表号、写入数据、删除数据
The detailed process of connecting MySQL with QT and outputting data to QT window tablewidget.
Use QT to connect to MySQL and create table numbers, write data, and delete data
[principles of database system] Chapter 4 advanced database model: Unified Modeling Language UML, object definition language ODL
JUC并发编程基础(4)--线程组和线程优先级
论文阅读-Endmember-Guided Unmixing Network (EGU-Net) 端元指导型高光谱解混网络
Headlong platform operation
MySQL foundation - constraints
UE4:浅谈什么是GamePlay框架
Foundation of JUC concurrent programming (4) -- thread group and thread priority
Qt新建工程简介
什么是单调栈
Vscode multiline comments always expand automatically
[principles of database system] Chapter 5 algebra and logic query language: package, extension operator, relational logic, relational algebra and datalog
Find the ArrayList < double > with the most occurrences in ArrayList < ArrayList < double >