1 条题解

  • 0
    @ 2025-3-16 20:49:36
    • 暴力会超时
    • 可以使用堆优化
    #include <bits/stdc++.h>
    using namespace std;
    const int N = 5e6 + 10;
    
    void solve1() {
        int n, k, x;
        char op;
        while (cin >> n >> k) {
            vector<int> v;
            while (n--) {
                cin >> op;
                if (op == 'I') {
                    cin >> x;
                    v.push_back(x);
                } else {
                    sort(v.begin(), v.end(), [](int l, int r) { return l >= r; });
                    cout << v[k - 1] << endl;
                }
            }
        }
    }
    void solve2() {
        int n, k, x;
        char op;
        while (cin >> n >> k) {
            priority_queue<int, vector<int>, greater<int>> q1;  // 小顶堆
            // priority_queue<int, vector<int>, less<int>> q2;     // 大顶堆
            while (n--) {
                cin >> op;
                if (op == 'I') {
                    cin >> x;
                    if (q1.size() < k)
                        q1.push(x);
                    else {
                        if (x > q1.top())
                            q1.pop(), q1.push(x);
                    }
                } else if (q1.size() == k)
                    cout << q1.top() << endl;
            }
        }
    }
    int main() {
        solve2();
    }
    
    • 1

    信息

    ID
    305
    时间
    1000ms
    内存
    256MiB
    难度
    9
    标签
    递交数
    26
    已通过
    2
    上传者