跳到正文

算法·理论

单调队列模板

Caiwenyu约 1 分钟
原文
文章信息与版本

文章信息

收录
2026年8月2日
更新
2026年8月2日
篇幅
约 1 分钟

版本

  1. v1当前
C++
//单调队列:长度固定的区间的最大值,最小值  
#include<bits/stdc++.h>
using namespace std;
int main()
{ 
	//最大值 
	for(int i=1;i<=n;i++)
	{
		if(tt>hh&&i-q[hh]+1>k) hh++;
		while(tt>hh&&a[q[tt-1]]<=a[i]) tt--;
		q[tt++]=i;
		cout<<q[hh];//i-k+1~i区间内的最大值  
	} 
	return 0;
}
原文