1 条题解

  • 0
    @ 2026-11-2 13:10:53

    音乐会的等待(Patrik) 题解

    思路分析

    按题目要求使用对应数据结构与算法模拟/递推求解,注意数据范围边界与时间复杂度。

    参考代码

    #include <iostream>
    #include <vector>
    using namespace std;
    
    int main() {
        ios::sync_with_stdio(false);
        cin.tie(nullptr);
        int n;
        if (!(cin >> n)) return 0;
        vector<pair<long long, int>> stk;
        stk.reserve(n);
        long long ans = 0;
        for (int i = 0; i < n; ++i) {
            long long h;
            cin >> h;
            int cnt = 1;
            while (!stk.empty() && stk.back().first <= h) {
                ans += stk.back().second;
                if (stk.back().first == h) {
                    cnt += stk.back().second;
                }
                stk.pop_back();
            }
            if (!stk.empty()) ++ans;
            stk.push_back({h, cnt});
        }
        cout << ans << "\n";
        return 0;
    }
    
    • 1

    信息

    ID
    8628
    时间
    1000ms
    内存
    256MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者