【Stack】|Data Structure|Simulation Stack|Monotonic Stack|Use the stack as a data structure to process operations

One, the interpretation of the stack

The stack is a common data structure. We use the operation of the stack to complete some basic linear operations. We need to have a deep understanding of the operation of the stack and the scenarios it adapts to. It is divided into two parts, pointer and storage space q[N] . Usually, we can complete most of the basic operations with one pointer. The following figure shows a common stack structure.
insert image description here
The basic operations of the simulated stack include stack operation, stack operation, judging whether the top of the stack is a null operation, and returning to the top of the stack;

Push operation: move the pointer, assign a value
Pop operation: move the pointer, similar to the delete operation in the computer
Feature operation: whether it is empty or not, return to the top of the stack

The method of using the monotonic stack , we often use the monotonic stack in the same operation as the monotonic queue , of course, the data of the monotonic stack generally does not perform dequeue operations, so it may be easier for us to use, the queue stores the washing mark , the stack can not only store subscripts, but also store values. Of course, we can also store these values ​​by opening an nq[N] array in the queue. After learning the monotonic stack, we can bring the idea of ​​the monotonic stack into the monotonic queue. To complete the data storage
insert image description here

Second, the implementation of the simulation stack

The operations of the simulated stack include: creating the stack, popping the stack, pushing the stack, judging whether the stack is empty, and returning to the top of the stack

1, the creation of the stack

const int N = 100010 ;

int stk[N], tt  = - 1  ;
//stk为存储空间,存储函数值,和单调栈存储下标不同;
2. Push elements into the stack
void push (int u)
{
    
    
	stk[++ tt] = u ;
}
//入栈操作 , 把函数值压入栈中  
3. Pop elements from the stack
void pop ()
{
    
    
	tt -- ;
}
4, return whether the stack is empty
void empty() 
{
    
    
	return tt == - 1 ;
}
5, return the top element of the stack

int top () 
{
    
    
	return stk[tt] ;
}

The total function of the simulation stack

#include <iostream>

using namespace std ;

const int N = 100010 ;

int stk[N], tt  = - 1  ;
//stk为存储空间,存储函数值,和单调栈存储下标不同;

void push (int u)
{
    
    
	stk[++ tt] = u ;
}
//入栈操作 , 把函数值压入栈中  


void pop ()
{
    
    
	tt -- ;
}

void empty() 
{
    
    
	return tt == - 1 ;
}

int top () 
{
    
    
	return stk[tt] ;
}



int main ()
{
    
    
	return 0 ;
}

In the case that we have not learned stl , we can use the operation of this simulated stack to replace the operation of the stack.

Three, monotonic stack

1. Question template

Monotonic stack questions, under normal circumstances, are operated on values ​​that are closest to him and satisfy the size relationship. We should think of monotonic stack operations at the first point when we see near problems, and this kind of nearest problem is generally unidirectional. The undirected problem can sometimes be simulated using a monotonic stack .

2. Algorithm template

#include <iostream>
using namespace std ;

const int N = 100010 ;

int stk[N] , tt =  -1 ;

int main ()
{
    
    
    int n ; 
    cin >> n ;
    for(int i = 0 ; i < n ; i ++ )
    {
    
    
        int x ;
        cin >> x ;
        while (tt != - 1 && stk[tt] >= x )  tt -- ;
        //1.指定顺序关系弹出栈顶元素&&保证栈内有元素————保证栈的单调性
        if(tt == -1 )   cout << tt <<" ";
        else cout << stk[tt] << " " ; 
        stk[++ tt] = x ;
    }   
    cout << endl; 
    return 0 ; 
}

3. Algorithm thinking

In general, whether it is a monotonic stack or a monotonic queue, both of their data structures are an optimization idea, reducing traversal operations to complete the optimization of the entire algorithm, thereby reducing the number of traversal algorithms, although this is the algorithm The essential idea, but this idea is more obvious in these two data structures. These two operations are relatively simple algorithms that implement operations based on characteristics. They rely more on topics. Previously learned dichotomies, sorting, high precision...all There are complete templates and clear ideas. This data structure implements algorithmic operations through its own characteristics. For example, the monotonic stack uses the monotonicity of the topic . We can not only use its own minimum value characteristics like a heap , but also use it with the topic Use the characteristics of the topic in combination.

Guess you like

Origin blog.csdn.net/wen030803/article/details/131749065