当前位置:网站首页>Stack, monotone stack, queue, monotone queue
Stack, monotone stack, queue, monotone queue
2022-07-03 11:07:00 【ACM Association of North China University of Technology】
List of articles
One 、 What are stacks and queues ?
A stack is a last in, first out (Last in First out) The linear table , abbreviation LIFO, Insert and delete operations are only allowed at one end ;
A queue is a first in, first out (FIrst in First out) The linear table , abbreviation FIFO, Insertion is only allowed at one end , Delete at the other end .
1. Implementation principle of stack
The core operation of stack with stack is stack top stack 、 There are two operations of stack top and stack out , Pictured :
Yes 1 ,3 ,2 ,4 ,5 Perform the operations of entering and exiting the stack respectively

See here for the application and operation of stack
2.1 Monotonic stack
seeing the name of a thing one thinks of its function , A monotone stack is a stack with monotone properties
Right now 1 ,3 ,2 ,4 ,5 Perform simulation operations

- 1 When entering the stack , The stack is empty. , Direct stack , The elements in the stack are :1
- 3 When entering the stack , Top element of stack 1 Than 3 Small ,1 Out of the stack , The stack is empty. ,3 Push , The elements in the stack are :3
- 2 When entering the stack , Top element of stack 3 Than 2 Big , Direct stack , The elements in the stack are :3,2
- 4 When entering the stack , Top element of stack 2 Than 4 Small ,2 Out of the stack , Top element of stack 3 Than 4 Small ,3 Out of the stack , The stack is empty. ,4 Push , The elements in the stack are :4
- 5 When entering the stack , Top element of stack 4 Than 5 Small ,4 Out of the stack , The stack is empty. ,5 Push , The elements in the stack are 5
Pseudo code
for ( Traversal array ){
if ( The stack is empty || Top element of stack >= The current element ){
Push ;
} else {
while ( Stack is not empty. && Top element of stack < The current element ){
Stack top element out of stack ;
}
The current element is put on the stack ;
}
}
2.3 Application example
Title source :acwing 830. Monotonic stack
Title Description :
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≤105
1≤ The elements in the sequence ≤109
sample input
5
3 4 2 7 5
sample output
-1 3 -1 2 2
Analog monotone stack :
#include <iostream>
using namespace std;
const int N=100010;
int n;
int skt[N],tt;
int main()
{
cin>>n;
while(n--)
{
int x;
cin>>x;
while(tt&&skt[tt]>=x) tt--;
if(tt) cout<<skt[tt]<<' ';
else cout<<-1<<' ';
skt[++tt]=x;
}
return 0;
}
3.1 queue
Yes 1, 3, 2, 4, 5 The simulation operation is shown in the figure 
3.2 Application example
Title source :acwing 829. Simulation queue
Title Description :
Implement a queue , The queue is initially empty , Four operations are supported :
- push x – Insert a number at the end of the line x;
- pop – Pop a number from the head of the team ;
- empty – Determines if the queue is empty ;
- query – Query the queue header element .
Now we're going to... The queue M Operations , Each of these operations 3 And operation 4 Output the corresponding results .
Input format
The first line contains integers M, Indicates the number of operations .
Next M That's ok , Each line contains an action command , The operation command is push x,pop,empty,query One of the .
Output format
For each empty and query All operations need to output a query result , Each result takes up one line .
among ,empty The query result of the operation is YES or NO,query The query result of the operation is an integer , Represents the value of the queue header element .
Data range
1≤M≤100000,
1≤x≤109,
All operations are legal .
sample input
10
push 6
empty
query
pop
empty
push 3
push 4
pop
query
push 6
sample output
NO
6
YES
4
Don't use stl
#include <iostream>
using namespace std;
const int N=100010;
int hh,tt=-1;
int q[N];
int main ()
{
int x,m;
string op;
cin>>m;
while(m--)
{
cin>>op;
if(op=="push")
{
cin>>x;
q[++tt]=x;
}
else if(op=="pop") hh++;
else if(op=="empty")
{
if(hh<=tt) cout<<"NO"<<endl;
else cout<<"YES"<<endl;
}
else cout<<q[hh]<<endl;
}
return 0;
}
Use stl
#include <iostream>
#include <queue>
using namespace std;
queue<int> q;
int main()
{
int x,m;
string op;
cin>>m;
while(m--)
{
cin>>op;
if(op=="push")
{
cin>>x;
q.push(x);
}
else if(op=="pop") q.pop();
else if(op=="empty")
{
if(q.size()) cout<<"NO"<<endl;
else cout<<"YES"<<endl;
}
else cout<< q.front() <<endl;
}
return 0;
}
4.1 A monotonous queue
Same as monotone stack , Monotone queues are queues with monotone properties
As shown in the figure 1, 3, 2, 4, 5 Perform monotonic queue operation 
To take the minimum value , Maintain a monotonically increasing queue
- 1 When you join the team , The queue is empty , Join the team directly , Elements in the team :1
- 3 When you join the team , Team tail element 1 Less than 3, Join the team directly , Elements in the team :1,3
- 2 When you join the team , Team tail element 3 Greater than 2, Fork out 3, Team tail element 1 Less than 2,2 The team , Elements in the team :1,2
- 4 When you join the team , Team tail element 2 Less than 4,4 The team , Elements in the team :1,2,4
- 5 When you join the team , Team tail element 4 Less than 5,5 The team , Elements in the team :1,2,4,5
4.2 Application example
Title source :acwing 154. The sliding window
Title Description :
Given a size of n≤10^6 Array of .
There is a size of k Sliding window of , It moves from the far left to the far right of the array .
You can only see in the window k A digital .
Move the window one position to the right each time .
Here is an example :
The array is [1 3 -1 -3 5 3 6 7],k by 3.
| window position | minimum value | Maximum |
|---|---|---|
| [1 3 -1] -3 5 3 6 7 | -1 | 3 |
| 1 [3 -1 -3] 5 3 6 7 | -3 | 3 |
| 1 3 [-1 -3 5] 3 6 7 | -3 | 5 |
| 1 3 -1 [-3 5 3] 6 7 | -3 | 5 |
| 1 3 -1 -3[ 5 3 6] 7 | 3 | 6 |
| 1 3 -1 -3 5[ 3 6 7] | 3 | 7 |
Your task is to make sure that the sliding window is in each position , Maximum and minimum values in the window .
Input format
The input contains two lines .
The first line contains two integers n and k, Represents the length of the array and the length of the sliding window .
The second line has n It's an integer , Represents the specific value of an array .
Separate peer data with spaces .
Output format
The output contains two .
The first line outputs , From left to right , Minimum value in each position slide window .
The second line outputs , From left to right , Maximum value in each position slide window .
sample input
8 3
1 3 -1 -3 5 3 6 7
sample output
-1 -3 -3 -3 3 3
3 3 5 5 6 7
yxc’s solution
#include <iostream>
#include<algorithm>
using namespace std;
const int N=1000010;
int n,k;
int a[N],q[N];
int main()
{
scanf("%d%d",&n,&k);
for (int i=0;i<n;i++) scanf("%d",&a[i]);
int hh=0,tt=-1;
for (int i=0;i<n;i++)
{
if(hh <= tt && q[hh] < i-k+1) hh++;
while(hh <= tt && a[q[tt]]>=a[i]) tt--;
q[++tt]=i;
if(i>=k-1) printf("%d ",a[q[hh]]);
}
puts("");
hh=0,tt=-1;
for (int i=0;i<n;i++)
{
if(hh <= tt && q[hh] < i-k+1) hh++;
while(hh <= tt && a[q[tt]]<=a[i]) tt--;
q[++tt]=i;
if(i>=k-1) printf("%d ",a[q[hh]]);
}
return 0;
}
边栏推荐
- EPS电动转向系统分析
- Qt:qss custom QSlider instance
- Qt:qss custom qheaderview instance
- Flink -- built in function (all)
- How can UI automated testing get out of trouble? How to embody the value?
- QT:QSS自定义 QSpinBox实例
- I have been doing software testing for three years, and my salary is less than 20K. Today, I put forward my resignation
- 11. Provider service registration of Nacos service registration source code analysis
- 如何让让别人畏惧你
- QT:QSS自定义 QTreeView实例
猜你喜欢
随机推荐
Flink chain conditional source code analysis
最高月薪18K 拥有好的“心态和选择”, 成功就差“认真和坚持”~
11. Provider service registration of Nacos service registration source code analysis
测试理论概述
QT:QSS自定义 QProgressBar实例
年中了,准备了少量的自动化面试题,欢迎来自测
程序进程管理工具-go supervisor
QT: QSS custom qtabwidget and qtabbar instances
软件测试必学基本理论知识——APP测试
Large scale e-commerce project - environment construction
在职美团测试工程师的这八年,我是如何成长的,愿技术人看完都有收获
The normal one inch is 25.4 cm, and the image field is 16 cm
First line of code kotlin notes
嵌入式软件测试怎么实现自动化测试?
Day 7 small exercise
Flink < --> how to use redis +with parameter
Cause: org. apache. ibatis. builder. Builderexception: error parsing SQL mapper configuration problem analysis
使用ML.NET+ONNX预训练模型整活B站经典《华强买瓜》
Probability theory: application of convolution in calculating moving average
Qt:qss custom qpprogressbar instance








