結果

問題 No.649 ここでちょっとQK!
ユーザー もりをもりを
提出日時 2020-08-25 18:56:56
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 265 ms / 3,000 ms
コード長 1,928 bytes
コンパイル時間 1,972 ms
コンパイル使用メモリ 202,016 KB
実行使用メモリ 7,384 KB
最終ジャッジ日時 2024-04-24 04:45:19
合計ジャッジ時間 6,216 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 265 ms
5,376 KB
testcase_04 AC 176 ms
7,384 KB
testcase_05 AC 165 ms
6,152 KB
testcase_06 AC 170 ms
6,728 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 92 ms
5,376 KB
testcase_13 AC 89 ms
5,376 KB
testcase_14 AC 87 ms
5,376 KB
testcase_15 AC 90 ms
5,376 KB
testcase_16 AC 91 ms
5,376 KB
testcase_17 AC 103 ms
5,376 KB
testcase_18 AC 112 ms
5,376 KB
testcase_19 AC 125 ms
5,376 KB
testcase_20 AC 137 ms
5,376 KB
testcase_21 AC 143 ms
5,376 KB
testcase_22 AC 151 ms
5,376 KB
testcase_23 AC 164 ms
5,376 KB
testcase_24 AC 172 ms
5,376 KB
testcase_25 AC 180 ms
5,376 KB
testcase_26 AC 188 ms
5,376 KB
testcase_27 AC 2 ms
5,376 KB
testcase_28 AC 2 ms
5,376 KB
testcase_29 AC 2 ms
5,376 KB
testcase_30 AC 55 ms
5,376 KB
testcase_31 AC 61 ms
5,376 KB
testcase_32 AC 2 ms
5,376 KB
testcase_33 AC 1 ms
5,376 KB
testcase_34 AC 2 ms
5,376 KB
testcase_35 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

template <typename T>
struct LRPrique {
    using F = function<int(int)>;
    const F f;
    const T err;
    priority_queue<T> L;
    priority_queue<T, vector<T>, greater<>> R;
    int L_sz, R_sz;
    T L_sum, R_sum;
    // f(sz) -> size of L
    // e.g.) median : [](int sz) -> int { return (sz + 1) / 2; }
    LRPrique(F f, T err = numeric_limits<T>::max())
        : f(f), err(err), L_sz(0), R_sz(0), L_sum(0), R_sum(0) {}
    void L_push(T x) {
        ++L_sz;
        L_sum += x;
        L.emplace(x);
    }
    void R_push(T x) {
        ++R_sz;
        R_sum += x;
        R.emplace(x);
    }
    T L_pop() {
        T ret = L.top();
        L.pop();
        --L_sz;
        L_sum -= ret;
        return ret;
    }
    T R_pop() {
        T ret = R.top();
        R.pop();
        --R_sz;
        R_sum -= ret;
        return ret;
    }
    void emplace(T x) {
        if (R.empty() or R.top() > x)
            L_push(x);
        else
            R_push(x);
    }
    void flatten() {
        int gL_sz = f(L_sz + R_sz);
        while (L.size() < gL_sz) {
            T tp = R_pop();
            L_push(tp);
        }
        while (L.size() > gL_sz) {
            T tp = L_pop();
            R_push(tp);
        }
    }
    T get() {
        int gL_sz = f(L_sz + R_sz);
        if (gL_sz > L_sz + R_sz) return err;
        flatten();
        return L.top();
    }
};

signed main() {

    int Q, K;
    cin >> Q >> K;

    LRPrique<ll> lr([=](int sz) -> int { return K; });

    while (Q--) {
        int T;
        cin >> T;
        if (T == 1) {
            ll a;
            cin >> a;
            lr.emplace(a);
        } else {
            ll ret = lr.get();
            if (ret == lr.err) {
                cout << -1 << endl;
            } else {
                cout << ret << endl;
                lr.L_pop();
            }
        }
    }
}
0