1.STL_queue
size() 返回队列的元素数 O(1)
push(x) 向队列中添加 x O(1)
pop() 在队列中取出队头元素并删除 O(1)
front() 返回队头元素 O(1)
empty() 在队列为空是返回 true O(1)
2.数组模拟循环队列
int head=0,tail=0,q[N];
//清空队列
void init_queue()
{
head=0;
tail=0;
}
//判断队列是否为空
bool isEmpty()
{
return head==tail;
}
//判断队列是否已满
bool isFull()
{
return head==(tail+1)%Max;
}
//入队
void push_q(int x)
{
if(!isFull)
{
q[tail]=x;
if(tail+1==Max) tail=0;
else tail++;
}
}
//出队
auto pop_q()
{
if(!isFull)
{
auto x=q[head];
head = (head+1) == MAX ? 0 :(head+1)
return x;
}
return 0;
}
3.单调队列
性质:
<1>队列中的元素在原来的列表中的位置是由前往后的
<2>队列中的元素的大小是 递增 或 递减 的
<3>从队首出列 从队首或队尾入列
例题:
#include<iostream>
using namespace std;
const int N = 1e6 + 10;
int n, k, q[N], a[N];//q[N]存的是数组下标
int main()
{
int tt = -1, hh=0;//hh队列头 tt队列尾
cin>>n>>k;
for(int i = 0; i <n; i ++) cin>>a[i];
for(int i = 0; i < n; i ++)
{
//维持滑动窗口的大小
//当队列不为空(hh <= tt) 且 当当前滑动窗口的大小(i - q[hh] + 1)>我们设定的
//滑动窗口的大小(k),队列弹出队列头元素以维持滑动窗口的大小
if(hh <= tt && k < i - q[hh] + 1) hh ++;
//构造单调递增队列
//当队列不为空(hh <= tt) 且 当队列队尾元素>=当前元素(a[i])时,那么队尾元素
//就一定不是当前窗口最小值,删去队尾元素,加入当前元素(q[ ++ tt] = i)
while(hh <= tt && a[q[tt]] >= a[i]) tt --;
q[ ++ tt] = i;
if(i + 1 >= k) printf("%d ", a[q[hh]]);
}
puts("");
hh = 0,tt = -1;
for(int i = 0; i < n; i ++)
{
if(hh <= tt && k < i - q[hh] + 1) hh ++;
while(hh <= tt && a[q[tt]] <= a[i]) tt --;
q[ ++ tt] = i;
if(i + 1 >= k ) printf("%d ", a[q[hh]]);
}
return 0;
}