最后一个大于

解题思路
单调栈模板题。
code
#include<iostream>
#include<cstdio>
#define int long long
#define mp make_pair
#define fst first
#define scd second
using namespace std;
int n,top;
int a[3000010];
int ans[3000010];
pair<int,int> b[3000010];
signed main()
{
cin>>n;
for(int i=1;i<=n;i++)
scanf("%lld",&a[i]);
for(int i=n;i;i--)
{
while(top&&a[i]>=b[top].fst) top--;
ans[i]=(top?b[top].scd-i:0);
b[++top]=mp(a[i],i);
}
for(int i=1;i<=n;i++)
printf("%lld ",ans[i]);
}

本文介绍了一道经典的单调栈算法题目,通过构建单调栈来高效解决求解数组中每个元素右侧最近且值大于该元素的问题。代码实现简洁明了,便于理解。
3906

被折叠的 条评论
为什么被折叠?



