当前位置:网站首页>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;
}
边栏推荐
- Restcloud ETL extracting dynamic library table data
- How do yaml files and zmail collide with the spark of the framework, and how can code and data be separated gracefully?
- Application service access configuration parameters
- The country has made a move! Launch network security review on HowNet
- Overall planning and construction method of digital transformation
- About whether arm's large and small end mode is related to CPU or compiler
- An analysis of the comments on the TV series Douban by procedural apes
- Eight recommended microservice testing tools
- 腾讯云荣获“可信云技术最佳实践-虚拟化”
- What are the reasons for the abnormal playback of the online channel of the channel accessed by easycvr national standard protocol?
猜你喜欢

Three indicators to help you measure the effectiveness of digital transformation

Nine practical guidelines for improving responsive design testing

Three layer switching experiment

Wechat applet to realize stacked rotation

How can programmers reduce bugs in development?

Crmeb multi merchant PC packaging tutorial

Get the actual name of the method parameter through the parameter

Flutter dart regular regexp matches non printing characters \cl\cj\cm\ck

How to start cloud native application development

Redis learning -- list of redis operations
随机推荐
Does the wave of layoffs in Chinese enterprises in 2021 need to be "judged" by morality?
Application service access configuration parameters
Eight recommended microservice testing tools
13 ways to reduce the cost of cloud computing
Leetcode question 136 [single number]
Nine practical guidelines for improving responsive design testing
Ten software development indicators for project managers
Uniapp wechat applet calls mobile map to navigate to the target point
Application service access configuration parameters
Failure analysis | database failure MHA is not switched
Using flex to implement common layouts
Number of occurrences of numbers in the array (medium difficulty)
基于BGP实现纯三层容器网络方案
Digital trend analysis of B2B e-commerce market mode and trading capacity in electronic components industry
About swagger
腾讯云荣获“可信云技术最佳实践-虚拟化”
Exception: Gradle task assembleDebug failed with exit code 1
Mental models: the best way to make informed decisions - farnam
如何在 R 中使用 Fisher 的最小显着性差异 (LSD)
Six configuration management tools that administrators must know