64,183
社区成员




代码:
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N=3e6+10;
int n,a[N],f[N];
stack<pair<int,int>>s;
signed main(){
cin>>n;
for(int i=1;i<=n;i++) cin>>a[i];
f[n]=0;
s.push({a[n],n});
for(int i=n-1;i>=1;i--){
while(s.size() && s.top().first<=a[i]) s.pop();
if(!s.size()) f[i]=0;
else f[i]=s.top().second;
s.push({a[i],i});
}
for(int i=1;i<=n;i++) cout<<f[i]<<' ';
return 0;
}//用STL要注意栈是不是空的,不然容易RE