2026.4.25课堂总结(单调栈)
P5788 【模板】单调栈
思路
模板题
代码
#include <bits/stdc++.h>
using namespace std;
stack<int> st;
int a[3000005],ans[3000005];
int main()
{
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
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--)
{
while(!st.empty()&&a[st.top()]<=a[i])
{
st.pop();
}
if(st.empty())
{
ans[i]=0;
}
else
{
ans[i]=st.top();
}
st.push(i);
}
for(int i=1;i<=n;i++)
{
cout << ans[i]<<' ';
}
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;
}
P1901 发射站
思路
这道题的题意就是每个发射站
| 情况 | 栈顶元素 vs 当前元素 | 含义 |
|---|---|---|
| 栈顶更矮 | height[st.top()] < height[i] |
当前站是栈顶的「右侧最近更高站」,栈顶的能量被当前站接收,栈顶出栈 |
| 栈顶更高 | height[top] > height_i |
当前站是栈顶的「左侧最近更高站」,当前站的能量被栈顶接收 |
代码
#include <bits/stdc++.h>
using namespace std;
#define int long long
stack<int> s;
struct node
{
int id;
int h;
int v;
int w;
} a[1000005];
signed main()
{
int n;
cin >> n;
for (int i = 1; i <= n; i++)
{
cin >> a[i].h >> a[i].v;
a[i].id = i;
}
for (int i = 1; i <= n; i++)
{
//被弹出的站他们向右发射的能量全部被当前站 i 接收
while (!s.empty() && a[s.top()].h < a[i].h)
{
a[i].w += a[s.top()].v;// 当前站累加被弹栈站的能量
s.pop();// 弹出
}
//如果弹栈后栈非空,说明栈顶比当前站更高
//当前站向左发射的能量被这个栈顶接收
if (!s.empty())
{
a[s.top()].w += a[i].v;// 栈顶累加当前站的能量
}
//入栈
s.push(i);
}
int maxn = 0;
for (int i = 1; i <= n; i++)
{
maxn = max(maxn, a[i].w);
}
cout << maxn << endl;
return 0;
}
P8082 [COCI 2011/2012 #4] KEKS
思路
这道题在
- 用单调栈遍历数字,从高位到低位依次入栈。
- 如果当前数字比栈顶大,且还能删除,就弹出栈顶(删除一个较小的高位),腾出空间让更大的数字占据更高位置。
- 最终栈中留下的就是最优解。若删除次数没用完,从末尾删除即可(保证递减)。
- 最后要倒序输出。
代码
#include <bits/stdc++.h>
using namespace std;
string s;
stack<int> st;
int main()
{
int n, k;
cin >> n >> k;
cin >> s;
for (int i = 0; i < n; i++)
{
//栈顶是较小的高位数字,删掉它让更大的 s[i] 占据更高位
while (k > 0 && !st.empty() && st.top() < s[i])
{
k--; // 用掉一次机会
st.pop(); // 弹出栈
}
st.push(s[i]); // 当前数字入栈
}
// 此时栈内为递减序列,末尾都是最小的,直接删掉即可
while (k > 0)
{
st.pop();
k--;
}
//栈转字符串
string ans = "";
while (!st.empty())
{
ans += st.top();
st.pop();
}
reverse(ans.begin(), ans.end()); // 翻转
cout << ans;
return 0;
}
P1823 [COI 2007] Patrik 音乐会的等待
思路
每对能互相看见的人,满足:较矮的人向两侧看,被最近的更高/等高者阻挡。
用单调栈模拟这个过程:
- 弹栈时,被弹出的每个人都与当前人配对 -> 他们之间无更高者阻挡
- 等高时,累加出现次数
代码
#include <bits/stdc++.h>
using namespace std;
#define int long long
struct node
{
int h;
int v;// 当前栈中该高度的连续出现次数
} a[500005];
stack<node> st;
signed main()
{
int n;
cin >> n;
for (int i = 1; i <= n; i++)
{
cin >> a[i].h;
a[i].v = 1;// 初始化
}
int ans = 0;// 能互相看见的对数
for (int i = 1; i <= n; i++)
{
//弹出所有比当前矮的
while (!st.empty() && a[i].h > st.top().h)
{
ans += st.top().v; // 栈顶所有人都与当前人配对
st.pop();
}
//栈顶与当前等高
if (!st.empty() && a[i].h == st.top().h)
{
node temp = st.top();
ans += st.top().v;// 先加上等高元素内部的配对
st.pop();// 弹出
// 弹出等高者后,如果栈非空,说明存在一个比当前一个更高的阻挡者
// 当前人能与这个更高者配对
if (!st.empty())
{
ans++;
}
// 入栈
st.push(node{temp.h, temp.v + 1});
}
// 栈顶比当前高
else
{
if (!st.empty())
{
ans++; // 与栈顶配对(+1)
}
st.push(a[i]);// 入栈
}
}
cout << ans;
return 0;
}
P2422 良好的感觉
思路
对于每个位置 i,我们假设它是区间 [L, R] 中的最小值。
那么,为了使舒适度 (min × sum) 最大,我们要找到:
- 左边第一个比
a_i 小的位置 ->记为left_i - 右边第一个比
a_i 小的位置 ->记为right_i
这样,以
然后计算该区间的和 sum = prefix[
代码
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 2e5 + 10;
int n, a[N];
int b[N], c[N];
int s[N];
stack<int> st;
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = 1; i <= n; i++)
{
cin >> a[i];
s[i] = s[i - 1] + a[i]; // 前缀和
}
//求 b[i](左边第一个更小的元素位置)
st.push(n);
for (int i = n - 1; i >= 1; i--)
{
// 弹栈
while (!st.empty() && a[st.top()] > a[i])
{
b[st.top()] = i; // st.top() 的左边界是 i
st.pop();
}
st.push(i);//入栈
}
// 左边没有更小的,左边界设为 0
while (!st.empty())
{
b[st.top()] = 0;
st.pop();
}
// 求 c[i](右边第一个更小的元素位置)
st.push(1);
for (int i = 2; i <= n; i++)
{
// 弹栈
while (!st.empty() && a[st.top()] > a[i])
{
c[st.top()] = i; // st.top() 的右边界是 i
st.pop();
}
st.push(i);
}
//右边没有更小的,右边界设为 n+1
while (!st.empty())
{
c[st.top()] = n + 1;
st.pop();
}
//枚举每个位置作为最小值
int ans = 0;
for (int i = 1; i <= n; i++)
{
// 以 a[i] 为最小值的最大区间是 [b[i] + 1, c[i] - 1]
int sum = s[c[i] - 1] - s[b[i]];
ans = max(ans, sum * a[i]);
}
cout << ans;
return 0;
}