题目传送门
#include <bits/stdc++.h>
using namespace std;
const int N = 8e4 + 5;
int n, h[N], ans;
stack<int>s;
int main() {
cin >> n;
for (int i = 1; i <= n; i++)
cin >> h[i];
for (int i = 1; i <= n; i++) {
while (!s.empty()) {
s.pop();
}
s.push(i);
for (int j = i + 1; j <= n; j++) {
if (s.size() == 1 && h[j] >= h[s.top()]) {
ans += j - s.top() - 1;
break;
}
while (h[j] >= h[s.top()] && s.size() > 1) {
s.pop();
}
s.push(j);
}
}
cout << ans;
return 0;
}