Looking cows
Monotonous stack
For the current per cow, to find the right closest to him, and larger than his cows
For each original problem into cows left to find the nearest, and smaller than his cows
Note that under the subject of storage
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
typedef pair<int,int> PII;
#define endl '\n'
stack<PII> stk;
int a[N],s[N],n;
int main() {
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
cin >> n;
for(int i = 1;i <= n; ++i) cin >> a[i];
for(int i = 1;i <= n; ++i) {
while(stk.size() && a[i] > stk.top().first) {
s[stk.top().second] = i;
stk.pop();
}
stk.push({a[i],i});
}
for(int i = 1;i <= n; ++i) cout << s[i] <<endl;
return 0;
}