当前位置:网站首页>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;
}
边栏推荐
- How MySQL works - Chapter 14
- 如何在 R 中执行稳健回归
- How about China Power Investment Xianrong futures? Is it safe to open futures accounts?
- Using flex to implement common layouts
- EasyGBS视频平台TCP主动模式拉流异常情况修复
- Architecture decryption from distributed to microservice: several common microservice architecture schemes
- Ten excellent business process automation tools for small businesses
- The country has made a move! Launch network security review on HowNet
- Uniapp wechat applet calls mobile map to navigate to the target point
- About pyqt5 to realize paging function (one window implements different interfaces)
猜你喜欢
About swagger
Millions of dollars worth of NFT were stolen in the attack, and Google issued an emergency warning to 3.2 billion users worldwide | February 21 global network security hotspot

How can an enterprise successfully complete cloud migration?

Two micro service interviews where small companies suffer losses

Software testing methods: a short guide to quality assurance (QA) models

Six configuration management tools that administrators must know

Mariana Trench, Facebook's open source code analysis tool

Number of occurrences of numbers in the array (medium difficulty)

【leetcode】838. Push domino (Analog)
![717.1-bit and 2-bit characters [sliding window]](/img/61/449566d2a8efbd403ae0361f839683.jpg)
717.1-bit and 2-bit characters [sliding window]
随机推荐
Seven strategies for successfully integrating digital transformation
Overall planning and construction method of digital transformation
About pyqt5 to realize paging function (one window implements different interfaces)
Is there a security risk in opening an account online? What to do if the business department opening an account nearby is far away from home. Is there any capital requirement for opening an account?
Continue to help enterprises' digital transformation -tce has obtained the certification of the first batch of digital trusted service platforms in China
Bigdecimalavoiddoubleconstructorrule: do not directly use the double variable as a parameter to construct BigDecimal
Regression testing strategy for comprehensive quality assurance system
2022 network security C module of the secondary vocational group scans the script of the surviving target aircraft (municipal, provincial and national)
浅谈云流送多人交互技术原理
How to decompile APK files
How about China Power Investment Xianrong futures? Is it safe to open futures accounts?
What makes data analysts good- Cassie Kozyrkov
Implementation of pure three-layer container network based on BGP
Mcu-08 interrupt system and external interrupt application
Gateway solves cross domain access
How does the video platform import the old database into the new database?
variable
Rapidssl getting started SSL certificate
R language Quantitative Ecology redundancy analysis RDA analysis plant diversity species data visualization
717.1-bit and 2-bit characters [sliding window]