結果

問題 No.649 ここでちょっとQK!
ユーザー kyuna
提出日時 2019-08-24 13:58:49
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 285 ms / 3,000 ms
コード長 1,241 bytes
コンパイル時間 682 ms
コンパイル使用メモリ 77,556 KB
実行使用メモリ 5,984 KB
最終ジャッジ日時 2024-10-14 12:44:14
合計ジャッジ時間 5,147 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 32
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
template<class T> using max_heap = priority_queue<T>;
template<class T> using min_heap = priority_queue<T, vector<T>, greater<T>>;

int main() {
    // 1, 2, ..., K, K + 1, ...
    // └─  small  ─┘ └─  large  ─┘
    max_heap<long long> small;
    min_heap<long long> large;
    int q, k; cin >> q >> k;
    while (q--) {
        int t; cin >> t;
        if (t == 1) {
            long long v; cin >> v;
            if (small.size() < k) {
                small.emplace(v);
                continue;
            }
            long long kth = small.top();
            if (v < kth) {
                small.pop();
                small.emplace(v);
                large.emplace(kth);
            } else {
                large.emplace(v);
            }
        } else {
            if (small.size() < k) {
                cout << -1 << endl;
                continue;
            }
            long long kth = small.top(); small.pop();
            cout << kth << endl;
            if (!large.empty()) {
                kth = large.top(); large.pop();
                small.emplace(kth);
            }
        }
    }
    return 0;
}
0