洛谷P5788 单调栈(模板)

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const int N=3e6+10;
 4 int n,a[N],s[N],ans[N],top;
 5 
 6 int main(){
 7     scanf("%d",&n);
 8     for(int i=1;i<=n;i++) scanf("%d",&a[i]);
 9     for(int i=1;i<=n;i++){
10         while(top!=0 && a[s[top]]<a[i])
11             ans[s[top--]]=i;
12         s[++top]=i; 
13     }
14     while(top) ans[s[top--]]=0;
15     for(int i=1;i<=n;i++) cout<<ans[i]<<" ";
16 }

存个板子,方便下次使用。

posted @ 2022-04-05 15:33  YHXo  阅读(59)  评论(0)    收藏  举报