#include<bits/stdc++.h> using namespace std; const int N=1000005; int n,ansl[N],ansr[N],a[N]; int f[N],r,x; int main() { cin>>n; for(int i=0;i<n;i++)cin>>a[i],ansl[i]=ansr[i]=-1; for(int i=0;i<n;i++) { while(r>0&&a[f[r]]>=a[i]) { x=f[r]; r--; ansl[x]=r>0?f[r]:-1; ansr[x]=i; } f[++r]=i; } while(r) { x=f[r]; r--; ansl[x]=r>0?f[r]:-1; } for(int i=0;i<n;i++)cout<<ansl[i]<<' '<<ansr[i]<<endl; return 0; }
算法讲解052【必备】单调栈-上_哔哩哔哩_bilibili
标签:int,ansr,基础,--,ansl,模板,哔哩,单调 From: https://www.cnblogs.com/zbyQIN/p/18538406