当前位置: 首页 > news >正文

单调栈和单调队列

一、单调栈

1、使用场景

解决元素左 / 右侧第一个比他大 / 小的数字。

2、原理解释

用栈解决,目标是栈顶存储答案。

以元素左侧第一个比他小为例:

(1)遍历顺序一定是从左向右。

(2)由于栈顶一定是答案,所以如果栈顶比遍历到的元素大或相等,那就是不合法的,此时一直出栈。 

(3)一直到栈空或栈顶比元素小,分别表示元素左边没有数比元素小和元素左边有数比元素小。更新答案。

(4)此时不管怎么样都把元素入栈,因为这个元素可能会是之后的答案。

3、代码

例题:单调栈

P5788 【模板】单调栈 - 洛谷

// https://www.luogu.com.cn/problem/P5788
#include "bits/stdc++.h"
using namespace std;
// 找到右边大的数
// 除非我遍历到的这个数比顶大我才一直出栈(循环解决),栈为空那就是右边没有比我大的我入栈,栈不为空那说说明右边有比我大的我入栈
const int N = 1e7;
int a[N];
int ans[N];int main()
{int n;cin >> n;for(int i = 1; i <= n; i++)cin >> a[i];stack<int> st;for(int i = n; i >= 1; i--){while(st.size() && a[st.top()] <= a[i])st.pop();if(st.empty()){st.push(i);ans[i] = 0;}else {ans[i] = st.top();st.push(i);}}for(int i = 1; i <= n; i++){cout << ans[i] << ' ';}return 0;
}

4、例题

发射站

P1901 发射站 - 洛谷

// https://www.luogu.com.cn/problem/P1901
#include "bits/stdc++.h"
using namespace std;
#define ll long long
// 两个数组:
// 左边离塔最近的高于这个塔的下标
// 右边离塔最近的高于这个塔的下标
// 两个栈分别找到
// 加能量应该是左边比塔高的那个塔加这个塔的能量,top += i
const int N = 1e6 + 10;int sum[N]; // 对应下标的塔接受的能量
int v[N]; // 塔的高度
int e[N]; // 塔的能量
int ans = 0;
int main()
{int n;cin >> n;for(int i = 1; i <= n; i++)cin >> v[i] >> e[i];stack<int> st1;stack<int> st2;for(int i = 1; i <= n; i++){while(st1.size() && v[i] >= v[st1.top()])st1.pop();if(st1.size()){sum[st1.top()] += e[i];}st1.push(i);}for(int i = n; i >= 1; i--){while(st2.size() && v[i] >= v[st2.top()])st2.pop();if(st2.size()){sum[st2.top()] += e[i];}st2.push(i);}for(int i = 1; i <= n; i++){ans = max(ans, sum[i]);}cout << ans;return 0;
}

二、单调队列

1、使用场景

滑动窗口极值问题。

2、原理解释

用队列,目标是队头是答案。

以滑动窗口中的极大值为例:

(1)因为队头是窗口中的最大值,所以一个元素要入队尾插那一定是尽可能向前走,即比元素小的或相等的(刚进来的元素不容易被淘汰,所以相等的老东西也要走)全部尾删。

(2)最后队为空或者尾删停下后,更新尾插。

(3)解决完入队逻辑之后还要解决数据过期,即不在窗口内的元素要清除,所以在每次插入新元素之后对于下标过期的数据循环头删。

3、代码

例题:单调队列

P1886 滑动窗口 /【模板】单调队列 - 洛谷

// https://www.luogu.com.cn/problem/P1886
#include "bits/stdc++.h"
using namespace std;
const int N = 1e6 + 10;
int a[N];
// 双端队列,存下标防止数据过期
// 最小值:队为空直接加,来的数比队尾大不一定就不是后面的答案尾插,来的数比队尾小或等于可以竞争这个范围内的最小值,所以一直尾删。每次最后清除所有不合法下标。
// 最大值:队为空直接加,来的数比队尾小不一定就不是后面的答案尾插,来的数比队尾大或等于可以竞争这个范围内的最大值,所以一直尾删。每次最后清除所有不合法下标。
int main()
{int n, k;deque<int> dq1;deque<int> dq2;cin >> n >> k;for(int i = 1; i <= n; i++)cin >> a[i];// 最小for(int i = 1; i <= n; i++){while(dq1.size() && a[i] <= a[dq1.back()]) dq1.pop_back();dq1.push_back(i);while(dq1.front() <= i - k)dq1.pop_front();if(i >= k)cout << a[dq1.front()] << ' ';}cout << endl;// 最大for(int i = 1; i <= n; i++){while(dq2.size() && a[i] >= a[dq2.back()]) dq2.pop_back();dq2.push_back(i);while(dq2.front() <= i - k)dq2.pop_front();if(i >= k)cout << a[dq2.front()] << ' ';}return 0;
}

4、例题

1、质量检测

P2251 质量检测 - 洛谷

// https://www.luogu.com.cn/problem/P2251
#include "bits/stdc++.h"
using namespace std;
const int N = 1e6 + 10;
int a[N];
int main()
{int n, m;cin >> n >> m;for(int i = 1; i <= n; i++)cin >> a[i];deque<int> dq;for(int i = 1; i <= n; i++){while(dq.size() && a[i] <= a[dq.back()])dq.pop_back();dq.push_back(i);while(dq.front() <= i - m)dq.pop_front();if(i >= m)cout << a[dq.front()] << endl;}return 0;
}

2、HISTOGRA - Largest Rectangle in a Histogram

SP1805 HISTOGRA - Largest Rectangle in a Histogram - 洛谷

// https://www.luogu.com.cn/problem/SP1805
#include <iostream>
#include <stack>
using namespace std;
typedef long long LL;
const int N = 1e5 + 10;
int n;
LL h[N];
LL x[N], y[N];
int main()
{
while(cin >> n, n)
{
for(int i = 1; i <= n; i++) cin >> h[i];
// 找左边,⼩
stack<int> st;
for(int i = 1; i <= n; i++)
{
// 单调递增的栈 - 存下标
while(st.size() && h[st.top()] >= h[i]) st.pop();
if(st.size()) x[i] = st.top();
else x[i] = 0;
st.push(i);
}
// 找右边,⼩
while(st.size()) st.pop();
for(int i = n; i >= 1; i--)
{while(st.size() && h[st.top()] >= h[i]) st.pop();
if(st.size()) y[i] = st.top();
else y[i] = n + 1;
st.push(i);
}
LL ret = 0;
for(int i = 1; i <= n; i++)
{
ret = max(ret, h[i] * (y[i] - x[i] - 1));
}
cout << ret << endl;
}
return 0;
}

相关文章:

  • C++(20): 文件输入输出库 —— <fstream>
  • 关于Android Studio for Platform的使用记录
  • Android Studio报错Cannot parse result path string:
  • 配置ssh服务-ubuntu到Windows拷贝文件方法
  • 远程主机状态监控-GPU服务器状态监控-深度学习服务器状态监控
  • 【C/C++】C++返回值优化:RVO与NRVO全解析
  • Java-反射(Reflection)
  • MoveIt Setup Assistant 在导入urdf文件的时候报错
  • math toolkit for real-time development读书笔记一三角函数快速计算(2)
  • JavaScript【4】数组和其他内置对象(API)
  • 清华大学大模型驱动的跨尺度空间智能研究最新综述:具身智能体、智慧城市和地球科学领域的进展
  • 跨国应用程序的数据存储方案常见的解决方案
  • 什么是PMBus
  • JS 高级程序设计 设计模式
  • 读写锁应用场景,适合读多写少
  • 设计模式 - 单例模式 - Tips
  • [ctfshow web入门] web77
  • OpenCV 特征检测全面解析与实战应用
  • AI知识梳理——RAG、Agent、ReAct、LangChain、LangGraph、MCP、Function Calling、JSON-RPC
  • 【滑动窗口】LeetCode 209题解 | 长度最小的子数组
  • 菲律宾中期选举结果揭晓,马科斯与杜特尔特家族重回“权力的游戏”
  • 女生“生理期请病假要脱裤子证明”?高校回应:视频经处理后有失真等问题
  • 嫩黑线货物列车脱轨致1名路外人员死亡,3人被采取刑事强制措施
  • 征稿启事|澎湃·镜相第三届非虚构写作大赛暨2026第六届七猫现实题材征文大赛
  • 贵州省委军民融合发展委员会办公室副主任李刚接受审查调查
  • 商务部:中方将适时发布中美经贸磋商相关消息