2026.4.18课堂总结(单调栈)
P1878 舞蹈课
思路
这道题可以使用小根堆维护所有当前相邻异性对,按舞蹈技术差值自动排序;用双向链表维护每个人的左右节点。
每次从堆顶取出差值最小的有效配对,将其删除,并将新形成的相邻异性对加入堆中,直到没有可配对的相邻异性为止。
代码
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N=2e5+10;
//{差值, {左编号, 右编号}}
priority_queue<pair<int,pair<int,int> >,vector<pair<int,pair<int,int> > >,greater<pair<int,pair<int,int> > > > q;
int n,a[N],is[N],ans1[N],ans2[N],nxt[N],pre[N];
int cnt=0;
string s;
signed main()
{
cin >> n;
cin>>s;
for(int i=1;i<=n;i++)
{
cin >> a[i];
}
// 将所有相邻异性对加入队列
for(int i=1;i<n;i++)
{
if(s[i-1]!=s[i])
{
q.push({abs(a[i]-a[i+1]),{i,i+1}});
}
}
// 初始化
for(int i=1;i<=n;i++)
{
nxt[i]=i+1;
pre[i]=i-1;
}
while(!q.empty())
{
auto t=q.top(); q.pop();
int x=t.second.first,y=t.second.second;
// 跳过已删除的无效配对
if(is[x]||is[y])
{
continue;
}
// 删除x和y,并连接其左右节点
nxt[pre[x]]=nxt[y];
pre[nxt[y]]=pre[x];
// 记录答案
ans1[++cnt]=x;
ans2[cnt]=y;
is[x]=is[y]=1;
// 若新相邻的两人是异性,加入堆
int L=pre[x],R=nxt[y];
if(L>=1 && R<=n && !is[L] && !is[R] && s[L-1]!=s[R-1])
{
q.push({abs(a[L]-a[R]),{L,R}});
}
}
cout<< cnt<<endl;
for(int i=1;i<=cnt;i++)
{
cout << min(ans1[i],ans2[i])<<" "<<max(ans1[i],ans2[i])<<endl;
}
return 0;
}
P5788 【模板】单调栈
思路
单调栈模板题。从右往左遍历数组,维护一个单调递增栈,栈中存储的是下标。对于每个位置 i,不断弹出栈顶元素,直到栈顶对应的值>
代码
#include <bits/stdc++.h>
using namespace std;
stack<int> st;
int a[3000005], ans[3000005];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
for (int i = 1; i <= n; i++)
{
cin >> a[i];
}
// 从右往左遍历
for (int i = n; i >= 1; i--)
{
// 弹出所有 <= a[i] 的元素
while (!st.empty() && a[st.top()] <= a[i])
{
st.pop();
}
// 栈顶就是右边第一个大于 a[i] 的元素下标,栈空则无解
ans[i] = st.empty() ? 0 : st.top();
st.push(i); // 将当前位置入栈
}
for (int i = 1; i <= n; i++)
{
cout << ans[i] << ' ';
}
return 0;
}
P2866 [USACO06NOV] Bad Hair Day S
思路
这道题可以先从左往右遍历每头牛,维护一个单调递减栈,栈中存储的是身高。对于当前牛 i(身高 t),不断弹出栈顶所有 ≤t 的元素——这些牛都比 t 矮或等高,会被挡住。弹出后,栈中剩余的元素就是比 t 高且在 t 左边的牛,而 t 能"看到"它们之间的所有牛,所以这些剩余元素的数量就是
代码
#include<bits/stdc++.h>
using namespace std;
int n, t;
long long ans;
stack<int> a;
int main()
{
cin >> n;
for (int i = 1; i <= n; i++)
{
cin >> t;
// 弹出所有 <= 当前身高的牛
while (!a.empty() && a.top() <= t)
{
a.pop();
}
// 栈中剩余的牛都比当前牛高,当前牛能看见它们之间的所有牛
ans += a.size();
a.push(t); // 当前牛入栈
}
cout << ans;
return 0;
}
T734899 奶牛的视野统计
思路
这题是 Bad Hair Day 的变体——不仅要输出总和,还要输出每头牛各自的
代码
#include<bits/stdc++.h>
using namespace std;
int n;
long long ans;
int x[10000005], t[1000005];
stack<int> a;
int main()
{
cin >> n;
for (int i = 1; i <= n; i++)
{
cin >> t[i];
}
// 从右往左遍历
for (int i = n; i >= 1; i--)
{
// 弹出所有比 t[i] 矮的牛
while (!a.empty() && t[a.top()] < t[i])
{
a.pop();
}
// 能看到的牛数 = 下一个屏障的位置 - 当前位置 - 1
if (a.empty())
{
x[i] = max(n - i - 1, 0);// 没有屏障,能看到所有右侧的牛
}
else
{
x[i] = max(a.top() - i - 1, 0); // 第一个 >= h[i] 的牛之前都可见
}
a.push(i);// 当前牛入栈
}
// 输出每头牛的 C_i,并累加总和
for (int i = 1; i<=n; i++)
{
cout << x[i] << " ";
ans += x[i];
}
cout << endl;
cout << ans;
return 0;
}
U514895 ١١(❛ᴗ❛)【单调栈】-左侧第一个较小值(不重复)
思路
求每个位置 i 左侧第一个比
代码
#include <bits/stdc++.h>
using namespace std;
#define int long long
stack<int> st;
int a[30000005], ans[30000005];
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
for (int i = 1; i <= n; i++)
{
cin >> a[i];
}
// 从左往右遍历
for (int i = 1; i <= n; i++)
{
// 弹出所有比 a[i] 大的
while (!st.empty() && a[st.top()] > a[i])
{
st.pop();
}
// 栈顶即为左边最近的较小值下标;栈空则不存在
ans[i] = st.empty() ? -1 : st.top();
st.push(i); // 当前位置入栈
}
// n>1000时输出异或值
if (n > 1000)
{
int x = ans[2];
for (int i = 3; i <= n; i++) x ^= ans[i];
cout << x;
return 0;
}
for (int i = 1; i <= n; i++)
{
cout << ans[i] << ' ';
}
return 0;
}
U514939 ١١(❛ᴗ❛)【单调栈】-左右两侧第一小于等于值(存在重复情况)
思路
求每个位置
- 左->右遍历:维护单调递增栈,弹出所有栈顶
> a_i 的元素,栈顶即为左侧第一个\leq a_i 的下标。 - 右->左遍历:维护单调递增栈,弹出所有栈顶
> a_i 的元素,栈顶即为右侧第一个\leq a_i 的下标。
代码
#include <bits/stdc++.h>
using namespace std;
stack<int> st, st2;
int a[500005], ans[500005], ans2[500005];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
for (int i = 1; i <= n; i++)
{
cin >> a[i];
}
//从左往右,求左侧第一个 <= a[i] 的下标
for (int i = 1; i <= n; i++)
{
// 弹出所有 > a[i] 的
while (!st.empty() && a[st.top()] > a[i])
{
st.pop();
}
ans[i] = st.empty() ? -1 : st.top();
st.push(i);
}
//从右往左,求右侧第一个 <= a[i] 的下标
for (int i = n; i >= 1; i--)
{
while (!st2.empty() && a[st2.top()] > a[i])
{
st2.pop();
}
ans2[i] = st2.empty() ? -1 : st2.top();
st2.push(i);
}
for (int i = 1; i <= n; i++)
{
printf("%d %d\n", ans[i], ans2[i]);
}
return 0;
}
P12241 [蓝桥杯 2023 国 C] 最大区间
思路
对每个位置
做法:两遍单调栈分别求出每个 l[i] 和右边第一个 r[i],则以
代码
#include<bits/stdc++.h>
using namespace std;
#define int long long
int a[30000005], l[30000005], r[30000005], ans;
stack<int> st;
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> a[i];
}
//从左往右,求 l[i] = 左侧第一个 < a[i] 的下标
for (int i = 0; i < n; i++)
{
// 弹出所有 >= a[i] 的
while (!st.empty() && a[st.top()] >= a[i])
{
st.pop();
}
l[i] = st.empty() ? -1 : st.top(); // -1 表示左边没有更小的
st.push(i);
}
// 清空栈
while (!st.empty())
{
st.pop();
}
//从右往左,求右侧的r[i] = 第一个 < a[i] 的下标
for (int i = n - 1; i >= 0; i--)
{
while (!st.empty() && a[st.top()] >= a[i])
{
st.pop();
}
r[i] = st.empty() ? n : st.top(); // n 表示右边没有更小的
st.push(i);
}
for (int i = 0; i < n; i++)
{
// 长度 = r[i] - l[i] - 1
ans = max(ans, a[i] * (r[i] - l[i] - 1));
}
cout << ans;
return 0;
}