这里的栈和队列不是STL中的栈和队列,是用数组模拟出来的
栈
先进后出
模板
int stk[N],tt=0;
// 插入
stk[++tt]=x;
// 弹出
tt--;
// 判断栈是否为空
if(tt>) not empty;
else empty;
// 栈顶
stk[tt];
模拟栈
实现一个栈,栈初始为空,支持四种操作:
(1) “push x” – 向栈顶插入一个数x;
(2) “pop” – 从栈顶弹出一个数;
(3) “empty” – 判断栈是否为空;
(4) “query” – 查询栈顶元素。
现在要对栈进行M个操作,其中的每个操作3和操作4都要输出相应的结果。
输入格式
第一行包含整数M,表示操作次数。
接下来M行,每行包含一个操作命令,操作命令为”push x”,”pop”,”empty”,”query”中的一种。
输出格式
对于每个”empty”和”query”操作都要输出一个查询结果,每个结果占一行。
其中,”empty”操作的查询结果为“YES”或“NO”,”query”操作的查询结果为一个整数,表示栈顶元素的值。
数据范围
1≤M≤100000,
1≤x≤109
所有操作保证合法。
输入样例:
10
push 5
query
push 6
pop
query
pop
empty
push 4
query
empty
输出样例:
5
5
YES
4
NO
代码样例
#include<iostream>
using namespace std;
const int N=100010;
int stk[N],tt;
// 向栈顶插入一个数
void push(int x)
{
stk[++tt]=x;
}
// 从栈顶弹出一个数
void pop()
{
tt--;
}
// 判断栈是否为空
int empty()
{
if(tt==0) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
return 0;
}
// 查询栈顶元素
int query()
{
cout<<stk[tt]<<endl;
return 0;
}
int main()
{
int n;
cin>>n;
while(n--)
{
string op;
int x;
cin>>op;
if(op=="push")
{
cin>>x;
push(x);
}
else if(op=="pop")
{
pop();
}
else if(op=="empty")
{
empty();
}
else if(op=="query")
{
query();
}
}
return 0;
}
队列
先进先出
模板
// 在队尾插入元素,在队尾弹出元素
int q[N],hh,tt=-1;
// 插入一个元素
q[++tt]=x;
// 弹出一个元素
hh++;
// 判断是否为空
if(hh<=tt) not empty;
else empyt;
// 取出队头元素
q[hh];
模拟队列
实现一个队列,队列初始为空,支持四种操作:
(1) “push x” – 向队尾插入一个数x;
(2) “pop” – 从队头弹出一个数;
(3) “empty” – 判断队列是否为空;
(4) “query” – 查询队头元素。
现在要对队列进行M个操作,其中的每个操作3和操作4都要输出相应的结果。
输入格式
第一行包含整数M,表示操作次数。
接下来M行,每行包含一个操作命令,操作命令为”push x”,”pop”,”empty”,”query”中的一种。
输出格式
对于每个”empty”和”query”操作都要输出一个查询结果,每个结果占一行。
其中,”empty”操作的查询结果为“YES”或“NO”,”query”操作的查询结果为一个整数,表示队头元素的值。
数据范围
1≤M≤100000,
1≤x≤109,
所有操作保证合法。
输入样例:
10
push 6
empty
query
pop
empty
push 3
push 4
pop
query
push 6
输出样例:
NO
6
YES
4
代码样例
#include<iostream>
using namespace std;
const int N=100010;
int q[N],tt=-1,hh;
// 向队尾插入一个数
void push(int x)
{
q[++tt]=x;
}
// 从队头弹出一个数
void pop()
{
hh++;
}
// 判断队列是否为空
void empty()
{
if(hh<=tt) cout<<"NO"<<endl;
else cout<<"YES"<<endl;
}
// 查询队头元素
void query()
{
cout<<q[hh]<<endl;
}
int main()
{
int n;
cin>>n;
while(n--)
{
string op;
int x;
cin>>op;
if(op=="push")
{
cin>>x;
push(x);
}
else if(op=="pop")
{
pop();
}
else if(op=="empty")
{
empty();
}
else if(op=="query")
{
query();
}
}
return 0;
}
单调栈
用的不多,直接上模板和例题
模板
常见模型:找出每个数左边离它最近的比它大/小的数
int tt = 0;
for (int i = 1; i <= n; i ++ )
{
while (tt && check(stk[tt], i)) tt -- ;
stk[ ++ tt] = i;
}
例题
单调栈
给定一个长度为N的整数数列,输出每个数左边第一个比它小的数,如果不存在则输出-1。
输入格式
第一行包含整数N,表示数列长度。
第二行包含N个整数,表示整数数列。
输出格式
共一行,包含N个整数,其中第i个数表示第i个数的左边第一个比它小的数,如果不存在则输出-1。
数据范围
1≤N≤105
1≤数列中元素≤109
输入样例:
5
3 4 2 7 5
输出样例:
-1 3 -1 2 2
代码样例
#include<iostream>
using namespace std;
const int N=100010;
int stk[N],tt;
int main()
{
int n;
cin>>n;
for(int i=0;i<n;i++)
{
int x;
cin>>x;
while(tt&&stk[tt]>=x) tt--;
if(tt) cout<<stk[tt]<<" ";
else cout<<"-1"<<" ";
stk[++tt]=x;
}
return 0;
}
单调队列
滑动窗口
给定一个大小为n≤106的数组。
有一个大小为k的滑动窗口,它从数组的最左边移动到最右边。
您只能在窗口中看到k个数字。
每次滑动窗口向右移动一个位置。
以下是一个例子:
该数组为[1 3 -1 -3 5 3 6 7],k为3。
窗口位置 | 最小值 | 最大值 |
---|---|---|
[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 |
您的任务是确定滑动窗口位于每个位置时,窗口中的最大值和最小值。
输入格式
输入包含两行。
第一行包含两个整数n和k,分别代表数组长度和滑动窗口的长度。
第二行有n个整数,代表数组的具体数值。
同行数据之间用空格隔开。
输出格式
输出包含两个。
第一行输出,从左至右,每个位置滑动窗口中的最小值。
第二行输出,从左至右,每个位置滑动窗口中的最大值。
输入样例:
8 3
1 3 -1 -3 5 3 6 7
输出样例:
-1 -3 -3 -3 3 3
3 3 5 5 6 7
代码样例
#include<iostream>
using namespace std;
const int N=1000010;
int a[N],q[N];
int main()
{
int n,k;
cin>>n>>k;
for(int i=0;i<n;i++) cin>>a[i];
int 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]]<<" ";
}
cout<<endl;
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]]<<" ";
}
return 0;
}