当前位置:网站首页>What is monotonic queue
What is monotonic queue
2022-07-24 06:12:00 【It's really all right, duck】
What is a monotonic queue
Monotonic queue refers to the monotonicity of the relationship between the elements in the queue , and , The head and tail of the team can be out of the team , Only the tail of the team can join the team .
The typical problem of monotonic queues is sliding windows , So take sliding window as an example to briefly introduce monotonic queue
Example
Given a size of n≤1e6 Array of .
There is a size of kk 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 7sample output :
-1 -3 -3 -3 3 3 3 3 5 5 6 7
Ideas
The idea of monotone queue and monotone stack is the same , Let's first think about how to do violence , Then delete the useless ones , We can get monotonicity , If there is monotonicity, let's find the extreme value , Then the time complexity of enumeration can be changed into O(1)
The practice of violence is that we use queues to maintain this window , Insert new elements at the end of the team every time , Elements that slide out bounce out of the tail of the team . Each time we find the extreme value, we directly traverse all the elements in the queue , This time complexity is O(k), So our whole time complexity is O(nk), The worst case scenario is O(n²). Then we will consider how to optimize , Similar to monotone stack , Let's consider whether some elements in the queue are useless , Then we delete these useless elements to see if we can get monotonicity .
Take the example , When our queue is 3,-1,-3 when , It can be found that as long as -3 In the queue , that 3,-1 It will not be output as an answer . So we can find , As long as the number at the front of our queue is larger than the number at the back , Then we can judge that the previous number must be useless , As long as we have this reverse order right , We can delete large numbers , Delete all such pairs in reverse order , The whole series will become a strictly monotonous rising series . What we seek is the minimum value in this queue , The minimum value of a strictly monotonically rising queue is our team leader , So every time you find the minimum value, you can directly find the team leader .
There is also the judgment of when the team leader element will leave the team ? For this question ,i Is the right endpoint of enumeration ,k Is the interval length , Note that there are subscripts in the queue , See if the subscript of the team leader is (i-k+1,i) Just within the scope .
So the monotone stack and monotone queue are similar , We all consider using stack and queue violence to solve this problem first , Then consider deleting useless elements to see if there is monotonicity , If the remaining elements are monotonic, we can consider optimization .
We also use arrays to simulate queues , Because it's faster .
Code implementation
#include <iostream>
using namespace std;
const int N=1e6+10;
int a[N],q[N];//a Represents the given array ,q Represents a queue
int main()
{
int n,k;
cin>>n>>k;
for(int i=0;i<n;i++)
cin>>a[i];
int hh=0,tt=-1;// Team head and tail
for(int i=0;i<n;i++)
{
// If the team leader has slipped out of the window
if(hh<=tt&&i-k+1>q[hh]) hh++;
// For the minimum , Determine whether the number of inserts is smaller than the tail element
while(hh<=tt&&a[q[tt]]>=a[i]) tt--;
q[++tt]=i;
// front k The number does not need to be output
if(i>=k-1)
cout<<a[q[hh]]<<" ";
}
cout<<endl;
hh=0,tt=-1;
// The maximum and minimum values are similar
for(int i=0;i<n;i++)
{
if(hh<=tt&&i-k+1>q[hh]) hh++;
// For maximum , Judge that the number of inserts is larger than the tail element
while(hh<=tt&&a[q[tt]]<=a[i]) tt--;
q[++tt]=i;
if(i>=k-1)
cout<<a[q[hh]]<<" ";
}
}边栏推荐
- Use QT to connect to MySQL and create table numbers, write data, and delete data
- Synergy LAN realizes multi host shared keyboard and mouse (AMD, arm)
- JDBC advanced -- learning from Shang Silicon Valley (DAO)
- The detailed process of connecting MySQL with QT and outputting data to QT window tablewidget.
- Unity(三)三维数学和坐标系统
- Foundation of JUC concurrent programming (8) -- read write lock
- Conversion of world coordinate system, camera coordinate system and image coordinate system
- 记一次高校学生账户密码的获取,从无到有
- unity2D游戏之让人物动起来-上
- Accessing a one-dimensional array with a pointer
猜你喜欢

Solve modularnotfounderror: no module named "cv2.aruco“

MySql下载,及安装环境设置

The detailed process of connecting MySQL with QT and outputting data to QT window tablewidget.

PDF Text merge

Foundation of JUC concurrent programming (6) -- lock lock

使用Keras和LSTM实现对于长期趋势记忆的时间序列预测-LSTNet
![[FatFs] migrate FatFs manually and transfer SRAM virtual USB flash disk](/img/fb/5f3d17f1f3d6e4979ece5126e2925e.png)
[FatFs] migrate FatFs manually and transfer SRAM virtual USB flash disk

Common features of ES6

Unicast, multicast, broadcast, tool development, introduction to QT UDP communication protocol development and source code of development tools

Jupyter notebook select CONDA environment
随机推荐
ue4 换装系统 2.换装系统的场景捕捉
JDBC elementary learning ------ (learning from Shang Silicon Valley)
day6-jvm
Dameng database_ Common user management commands
Unity基础知识及一些基本API的使用
Deepsort summary
day5-jvm
【数据库系统原理】第四章 高级数据库模型:统一建模语言UML、对象定义语言ODL
Day3 jvm+ sorting summary
精确计算时间延迟VxWorks 时间戳 详解
LSTM neural network
Vsual studio 2013 environment UDP multicast
JSON. Dumps() function parsing
使用Qt连接MySql并创建表号、写入数据、删除数据
bat批处理脚本、同时运行多个文件、按照顺序执行的批处理命令及xshell脚本。
记一次高校学生账户密码的获取,从无到有
Jestson installs IBus input method
[deep learning] handwritten neural network model preservation
Unity Shader :实现漫反射与高光反射
JS star scoring effect