#include <stdio.h>
#include <stack>
using namespace std;
struct node { int idx, h; };
int h[500010], sig[500010];
int main() {
int n;
scanf("%d", &n);
stack<node> st1, st2;
for(int i=1; i<=n; i++) {
scanf("%d", &h[i]);
st1.push({i, h[i]});
}
while(!st1.empty()) {
node S=st1.top();
st1.pop();
while(!st1.empty()&&st1.top().h<S.h) {
st2.push(st1.top());
st1.pop();
}
if(!st1.empty()) sig[S.idx]=st1.top().idx;
while(!st2.empty()) {
st1.push(st2.top());
st2.pop();
}
}
for(int i=1; i<=n; i++) printf("%d ", sig[i]);
}
Para incrustar este proyecto en tu sitio web, copia el siguiente código y pégalo en el HTML de tu sitio web: