思路:反向遍历Hi,用一个单调栈维护当前牛能仰望的最近的右对象的下标即可

#include <bits/stdc++.h>
using namespace std;

const int Nmax = 1e5+5;
int n,H[Nmax],tmp[Nmax];
stack<int> st;

int main(){
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);
    cin>>n;
    for(int i = 1;i <= n;i++)
        cin>>H[i];
    st.push(n);
    for(int i = n-1;i >= 1;i--){
        while(!st.empty() && H[i] >= H[st.top()]) st.pop();
        if(!st.empty()) tmp[i] = st.top();
        st.push(i);
    }
    for(int i = 1;i <= n;i++)
        cout<<tmp[i]<<'\n';
    return 0;
}