单调栈

单调栈

Winter Lv4

关于单调栈的一些东西

单调栈模板

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include<iostream>
#include<stack>
using namespace std;
const int N=3000010;
int a[N],f[N];
stack<int>s;
int n;
int main(void)
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
for(int i=n;i>=1;i--)
{
while(!s.empty()&&a[s.top()]<=a[i])
s.pop();
f[i]=s.empty()?0:s.top();
s.push(i);
}
for(int i=1;i<=n;i++)
printf("%d ",f[i]);

return 0;
}
  • Post title:单调栈
  • Post author:Winter
  • Create time:2023-03-16 19:09:14
  • Post link:https://spikeihg.github.io/2023/03/16/单调栈/
  • Copyright Notice:All articles in this blog are licensed under BY-NC-SA unless stating additionally.