【Queue】|Monotonic queue| Simulated queue| Queue creation and related usage

1. The basic definition and explanation of the queue

A queue is a simple data structure. First of all, we can know that a queue has two operations: output , input , and when we store related relationships, we also need to store related data structures, so we need to open An array unit to store the unit element of this queue. Then in order to complete this basic entry and exit operation, we can transform this data structure into a double-pointer data structure. We set a pointer at the head node and the tail node respectively to complete this data structure.

insert image description hereThe simulated queue in the queue is a series of operations that we use the array to simulate the data structure. This kind of operation is very simple. Generally speaking, what we can do with the queue can also be done with the simulated queue, so we usually use Calculations are performed by simulating a queue, and there is a monotonic queue. First of all, we need to understand the idea of ​​a monotonic queue. First of all, the sequence it stores is an ordered queue. Some values ​​in the entire array cannot be taken due to certain circumstances. In this way, we can reduce the data range very well. We usually do this type when the data range is relatively small, but the data is relatively large. We use the whole method to complete the data reduction and slimming operation.

Second, the simulated queue

As we all know, the simulation queue has several basic operations: entering the queue , leaving the queue , and judging whether it is empty

1, the creation of the queue
const int  N  = 100010 ; 

int q[N] , hh , tt = - 1 ;
2, the dequeue operation of the queue
tt ++ ;
3, Queue enqueue operation
int x ;
cin >> x; 
q[++ tt] = x ;
4, the operation of judging whether the queue is empty
return tt < hh ; 

Three, monotonic queue operation

1. Algorithm introduction

The emergence of monotonic queues is to solve problems similar to sliding windows. We need to solve some maximum value operations in a certain area. This area is an area smaller than the largest area. Generally speaking, this area is constant, and in this area In or in the data, we can know that it has a certain order relationship. For the time being, we will express this data relationship in numerical value as cnt value. We store this value in the queue in order, and output these values ​​​​when appropriate. .

2. Algorithm ideas

Quick counting: record how much data is in the [n,m] interval: m - n + 1 can complete the calculation

  1. The first thing we need to think about is how long the queue is, and when should we hank the data at the head—we generally use this i - hh + 1 >= m (the current number of digits is greater than or equal to the queue capacity Stored maximum value) when performing this operation
  2. At the same time, what we have to consider is how we can ensure that the data is stored monotonously - in general, we ensure that the queue is not empty hh <= tt and what we need to ensure is that the cnt value at the end of the queue does not satisfy monotonicity

3. Regular code

#include <iostream>
#include <algorithm>
using namespace std ;

const int N = 1000100 ;

int q[N] ,hh , tt = - 1 , g[N];

int main ()
{
    
    
    int n , m ; 
    cin >> n >> m ;
    for(int i = 0 ; i < n ; i ++ )
    {
    
    
        int x ;
        cin >> x ;
        g[i] =  x; 
      //  cout << i - q[hh] + 1 << endl ; 
        if(i - q[hh] + 1 > m)      hh ++ ;
        while(hh <= tt && x >= q[tt])   tt -- ;
        q[++ tt ] = i ;
        if(i >=  m - 1)     cout << g[q[hh]] << " ";  
    
}

    return 0 ;
}

This operation is related to the operation of finding the maximum value in the sliding window. What we are looking at is a simple monotonic queue operation for perfect numerical comparison. You can have a deeper understanding based on the code.

Relevant supplements will also be made when encountering related board questions in the future
; ——————————————

Guess you like

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