当前位置:网站首页>Stack and queue
Stack and queue
2022-06-25 14:58:00 【Caramel K】
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 ≤ 10^5
1 ≤ The elements in the sequence ≤ 10^9
sample input :
5
3 4 2 7 5
sample output :
-1 3 -1 2 2
Their thinking :
If you follow the most simple idea —— Violent settlement , So I have to write two for loop , Find No i The number nearest to it and smaller than itself .
Of course not hard to find , If in front of i In number , There are two numbers respectively x and y,x>y, also y The position is in x Behind , Then we just need to use y With the first i Just compare the numbers . therefore , We can judge when we input , If the number entered is larger than the top of the stack , Then the number at the top of the stack is output ; If the number entered is smaller than the number at the top of the stack , Then it becomes the new top of the stack , And the output -1.
such , Each number only needs to be put on the stack at most 、 Once out of the stack . The code is as follows :
#include<iostream>
using namespace std;
const int N=100010;
int n;
int stk[N],tt;//stk[] For the stack ,tt For the top of the stack
int main(){
cin>>n;
for(int i=0;i<n;i++)
{
int x;
cin>>x;
while(tt&&stk[tt]>=x)tt--;//tt Not empty And x Delete the top of the stack when it is less than or equal to the number at the top of the stack
if(tt)cout<<stk[tt]<<' ';// If the stack is not empty, the top of the stack is output
else cout<<-1<<' ';// Otherwise output -1
stk[++tt]=x;// Input x For the top of the stack
}
return 0;
}Another template question
Luogu P5788 【 Templates 】 Monotonic stack
Input
5 1 4 2 3 5
Output
2 5 4 5 0
explain / Tips
【 Data scale and agreement 】
about 30\%30% The data of ,n ≤ 100;
about 60\%60% The data of ,n ≤ 5 × 10^3;
about 100\%100% The data of ,1 ≤ n ≤ 3 × 10^6,1 ≤ ai ≤ 10^9.
The same idea as the question just now . The code is as follows :
#include<iostream>
using namespace std;
const int N=3e6+10;
int a[N],stk[N],ans[N];//a[] For input value ,stk[] For the stack ,ans[] Record subscripts
int tt=0;//tt For the top of the stack
int n,i;
int main(){
cin>>n;
for(i=1;i<=n;i++){
cin>>a[i];
}
for(i=1;i<=n;i++){
while(tt&&a[stk[tt]]<a[i])ans[stk[tt--]]=i;
// If the top of the stack is not empty , And the value corresponding to the top element of the stack is higher than the newly entered value a[i] Small , Delete the top of the stack , And record a[i] The subscript i
stk[++tt]=i;// here i For the stack top element
}
for(i=1;i<=n;i++){
if(i==1)cout<<ans[i];
else cout<<" "<<ans[i];
}
return 0;
} Of monotonic queues classic Water problem The template questions P1886 The sliding window
Title Description
There's a long one for n Sequence a, And a size of k The window of . Now this one slides from the left to the right , Slide one unit at a time , Find the maximum and minimum values in the window after each slide .
for example :
The array is [1,3,−1,−3,5,3,6,7], and k = 3.

Input format
There are two lines of input , The first line has two positive integers n,k. The second line n It's an integer , Represents a sequence a
Output format
The output consists of two lines , The first line is the minimum value of each window slide
The second line is the maximum value of each window sliding
Input
8 3 1 3 -1 -3 5 3 6 7
Output
-1 -3 -3 -3 3 3 3 3 5 5 6 7
explain / Tips
【 Data range 】
about 50% The data of ,1 ≤ n ≤ 10^5;
about 100% The data of ,1 ≤ k ≤ n ≤ 10^6,ai∈[-2^31,2^31).
Their thinking :
If you follow the idea of violence , Every k The number is searched once , Find the smallest ( Maximum ) Output .
But we can record this k The smallest number ( Maximum ) Number of numbers , Compare with the next number . If the next number is smaller , Then output the next number and write it into the queue ; conversely , Is to output the original minimum value . The code is as follows :
#include <iostream>
using namespace std;
const int N = 1000010;
int a[N], q[N];//a[] Is the original array ;q[] For queue , Indicates subscript
int main(){
int n, k;
cin>>n>>k;
for (int i = 0; i < n; i ++ ) cin>>a[i];
int hh = 0, tt = -1;//hh For the team leader ;tt For the end of the team
for (int i = 0; i < n; i ++ )
{
if (hh <= tt && i - k + 1 > q[hh]) hh ++ ;
// Pop up the team leader
while (hh <= tt && a[q[tt]] >= a[i]) tt -- ;
// If the next number is smaller than the number at the end of the team , Then replace the end of the queue with the next number
q[ ++ tt] = i;
if (i >= k - 1) cout<<a[q[hh]]<<" ";
// Output team leader
}
puts("");
// Maximum Empathy
hh = 0, tt = -1;
for (int i = 0; i < n; i ++ )
{
if (hh <= tt && i - k + 1 > q[hh]) hh ++ ;
while (hh <= tt && a[q[tt]] <= a[i]) tt -- ;
q[ ++ tt] = i;
if (i >= k - 1) cout<<a[q[hh]]<<" ";
}
puts("");
return 0;
}边栏推荐
- Async await to achieve sleep waiting effect
- Is it normal to dig for money? Is it safe to open a stock account?
- basic_ String mind map
- Disable scrolling in the iPhone web app- Disable scrolling in an iPhone web application?
- 90 后眼中的理想 L9:最简单的产品哲学,造最猛的爆款 | 指南斟
- 14 -- 验证回文字符串 Ⅱ
- QQ love talk candy love talk content acquisition and storage
- 2022年广东高考分数线出炉,一个几家欢喜几家愁
- 如何裁剪动图大小?试试这个在线照片裁剪工具
- [Ocean University of China] Data Sharing for retest of initial Examination
猜你喜欢

ffmpeg protocol concat 进行ts流合并视频的时间戳计算及其音画同步方式一点浅析

New good friend Pinia, leading the new era of state management

Kubernetes understands kubectl/ debugging

重磅!国产 IDE 发布,由阿里研发,完全开源!(高性能+高定制性)
![[try to hack] vulhub shooting range construction](/img/fc/6057b6dec9b51894140453e5422176.png)
[try to hack] vulhub shooting range construction

Use Matplotlib to draw a line chart

从0到1完全掌握 XSS

Clinical Chemistry | 张建中/徐健开发幽门螺杆菌单细胞精准诊疗技术

Using Sphinx to automatically generate API documents from py source files

关于win10 版本kicad 卡死的问题, 版本6.x
随机推荐
‘make_ unique’ is not a member of ‘std’
The best time to buy and sell stocks
Customization and encapsulation of go language zap library logger
System Verilog - function and task
Kubernetes understands kubectl/ debugging
Vs2019 scanf error
买基金在哪里开户安全?求指导
Learning notes on February 18, 2022 (C language)
Differences between member variables and local variables
分饼干问题
从0到1完全掌握 XSS
【Try to Hack】vulhub靶场搭建
【HBZ分享】LockSupport的使用
Build a minimalist gb28181 gatekeeper and gateway server, establish AI reasoning and 3D service scenarios, and then open source code (I)
全国首例,中国电信 5G 井下人员定位项目正式商用:可实时跟踪位置,保障作业安全
【Try to Hack】vulnhub DC1
Go语言Zap库Logger的定制化和封装使用详解
Qmake uses toplevel or topbuilddir
挖财是正规的吗?股票开户安全吗?
Learning notes on February 8, 2022 (C language)
