求思路
#include<bits/stdc++.h>
using namespace std;
int n,a[3000005],f[3000005];
stack<int> st;
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++) scanf("%d",&a[i]);
for(int i=n;i>=1;i--){
while(st.size()>=2&&a[st.top()]<=a[i]) st.pop();
if(st.empty()){
f[i]=-1;
continue;
}
int tmp=st.top();
st.pop();
if(st.empty()) f[i]=-1;
else if(a[st.top()]<=a[i]) f[i]=-1;
else f[i]=st.top();
st.push(tmp);
st.push(i);
}
for(int i=1;i<=n;i++){
if(f[i]==-1) printf("-1 ");
else printf("%d ",a[f[i]]);
}
return 0;
}