每日AC(每天22:00----23:00发AC)

每日AC(每天22:00----23:00发AC)

许诺 · 发布于 2025-03-11 · 浏览 393 回复 0
#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;

}

全部回复 共 0 条

还没有人回复。往下写第一条。

发表回复