https://cses.fi/alon/task/1645
#include <iostream>
#include <vector>
#include <stack>
using namespace std;
typedef long long ll;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<ll> arr(n);
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
stack<ll> stk;
vector<ll> res(n, 0);
for (int i = n - 1; i >= 0; i--) {
while (!stk.empty() && arr[i] < arr[stk.top()]) {
res[stk.top()] = i + 1;
stk.pop();
}
stk.push(i);
}
for (ll i : res) {
cout << i << ' ';
}
cout << '\n';
return 0;
}
Top comments (0)