利用单调递减栈的思想
#include <iostream>
using namespace std;
#include <stack>
#include <algorithm>
#include <vector>
int main() {
int n;
while (cin >> n) {
vector<int>v;
int m;
for (int i = 0; i < n; i++) {
cin >> m;
v.push_back(m);
}
vector<int>ltor(n);
vector<int>rtol(n);
stack<int>s1;
for (int i = 0; i < n; i++) {
ltor[i] = s1.size();
while (!s1.empty() && s1.top() <= v[i]) {
s1.pop();
}
s1.push(v[i]);
}
stack<int>s2;
for (int i = n - 1; i >= 0; i--) {
rtol[i] = s2.size();
while (!s2.empty() && v[i] >= s2.top()) {
s2.pop();
}
s2.push(v[i]);
}
for (int i = 0; i < n; i++) {
cout << ltor[i] + rtol[i] + 1 << " ";
}
cout << endl;
//system("pause");
}
return 0;
}