许诺 • 2个月前
#include <iostream>
#include <stack>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
stack<pair<long long, int>> stk;
long long res = 0;
for (int i = 0; i < n; ++i) {
long long h;
cin >> h;
int cnt = 1;
while (!stk.empty() && stk.top().first < h) {
res += stk.top().second;
stk.pop();
}
if (!stk.empty()) {
if (stk.top().first == h) {
res += stk.top().second;
cnt = stk.top().second + 1;
stk.pop();
}
if (!stk.empty()) {
res += 1;
}
}
stk.push({h, cnt});
}
cout << res << endl;
return 0;
}
评论:
请先登录,才能进行评论