当前位置:网站首页>Monotone stack template
Monotone stack template
2022-06-24 18:29:00 【One star accompanies the moon】
Monotonic stack
As the name suggests, the elements in the stack are monotonous .
But how to use this ? How can it be monotonous ? Look at the example below
subject
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.
sample input :
5
3 4 2 7 5
sample output :
-1 3 -1 2 2
The meaning of the topic is easy to understand
So let's first look at violent practices , Take one number at a time , Compare with the previous number
for(int i=0;i<n;i++)
{
for(int j=1;j=i;j++)
{
if(a[j]>=a[j-1])
{
break;
}
}
}
We think about the worst , When the sequence is in descending order , for example :
5
5 4 3 2 1
5 If you don't go through the cycle
4 Words and 5 Compare
3 And 4 and 5 Compare
2 And 3、4 and 5 Compare
1 And 2、3、4 and 5 Compare
Observation shows that , We need to take out each number , And then go through all the numbers before this number .
The time complexity is O(n^2),oj The data range is n<=1e5, Evaluation machine 1s The amount of internal operation data is 1e8~1e9, We are right. 1e10 The amount of data is unacceptable , So we need to optimize our practices . At this time, we introduce monotone stack .
Take the input sample as an example to simulate a single stack adjustment
3 4 2 7 5
Let's go through... In sequence , Look back from the past
First look at 3, No one smaller than him is put on the stack
Look again 4, Then compare it with the top element of the stack , Find that the conditions are met , Then output the top element of the stack , And then 4 Push
Look again 2, Also compare with the stack top element , But the top of the stack element ratio 2 Big , Then we need pop Out of the stack , Then compare it with the top element of the stack , Until we find the ratio 2 Small elements , Or the stack ends in empty time , Then we need to determine whether the stack is empty , Output when stack is empty -1, If the stack is not empty, it means that a comparison has been found 2 Small elements , That is, the output stack top element , The final will be 2 Push
Look again 7, Compare stack top elements , Less than the top of the stack element , Then enter the top element of the stack , Push
Finally, let's see 5, Compare pop-up stack top elements , Then compare , Find that the conditions are met , Output stack top elements
Let's take another look at the changing process of the elements in the stack
3
3 4
2
2 7
2 5
At this time, you will find that the elements in the stack are monotonically increasing from the bottom of the stack to the top of the stack . That's why it's called a monotone stack !!!
Template code
#include<iostream>
using namespace std;
const int N=1e5+5;
int a[N],stk[N],n,tt=0;
int main()
{
scanf("%d",&n);
for(int i=0;i<n;i++) scanf("%d",a+i);
for(int i=0;i<n;i++)
{
while(tt&&a[i]<=stk[tt]) --tt;
if(tt) printf("%d ",stk[tt]);
else printf("-1 ");
stk[++tt]=a[i];
}
return 0;
}
边栏推荐
- Business based precipitation component = & gt; manage-table
- Knowledge points of 2022 system integration project management engineer examination: ITSS information technology service
- Usage of typedef enum (enumeration)
- Digital transformation informatization data planning and technology planning
- About whether arm's large and small end mode is related to CPU or compiler
- R中的指数回归
- Three layer switching experiment
- 2022 network security C module of the secondary vocational group scans the script of the surviving target aircraft (municipal, provincial and national)
- Get max value of a bit column - get max value of a bit column
- Mysql database performance testing tool recommendation
猜你喜欢

What is decision intelligence?
R language Quantitative Ecology redundancy analysis RDA analysis plant diversity species data visualization

Wechat applet to realize stacked rotation

How to create simple shapes in illustrator 2022

国家出手了!对知网启动网络安全审查

Project Management Guide: tips, strategies and specific practices

Different JVM

13 ways to reduce the cost of cloud computing

Eight recommended microservice testing tools

13 skills necessary for a competent QA Manager
随机推荐
RestCloud ETL抽取动态库表数据实践
Selection (030) - what is the output of the following code?
How does the video platform import the old database into the new database?
Leetcode question 136 [single number]
Cloud service selection of enterprises: comparative analysis of SaaS, PAAS and IAAs
[quick news] the jeecgboot low code platform was successfully selected into the 2021 scientific innovation China · open source innovation list
Paper sharing | self supervised learning paper jointly released by Yann Lecun and read by engineers
Application service access configuration parameters
(Video + graphics) introduction to machine learning series - Chapter 11 support vector machines
Easygbs video platform TCP active mode streaming exception repair
How MySQL works - Chapter 14
Provide secure and convenient Oracle solutions for smart contract developers
Recommend a distributed JVM monitoring tool, which is very practical!
你知道CMDB吗?
Data driven decision making: Decision intelligence and design thinking
Tencent cloud won the "trusted cloud technology best practice - virtualization"
Leetcode topic [array] -216- combined sum III
[can you really use es] Introduction to es Basics (I)
Why are more and more people studying for doctors? Isn't it more and more difficult to graduate a doctor?
About pyqt5 to realize paging function (one window implements different interfaces)