結果

問題 No.649 ここでちょっとQK!
ユーザー HanahanaHanahana
提出日時 2023-06-27 17:41:02
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 294 ms / 3,000 ms
コード長 1,927 bytes
コンパイル時間 789 ms
コンパイル使用メモリ 77,512 KB
実行使用メモリ 5,688 KB
最終ジャッジ日時 2023-09-17 15:11:07
合計ジャッジ時間 5,589 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 294 ms
5,272 KB
testcase_04 AC 176 ms
5,688 KB
testcase_05 AC 162 ms
5,536 KB
testcase_06 AC 166 ms
5,520 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 91 ms
4,380 KB
testcase_13 AC 90 ms
4,376 KB
testcase_14 AC 88 ms
4,376 KB
testcase_15 AC 93 ms
4,380 KB
testcase_16 AC 92 ms
4,380 KB
testcase_17 AC 105 ms
4,476 KB
testcase_18 AC 114 ms
4,852 KB
testcase_19 AC 124 ms
4,380 KB
testcase_20 AC 133 ms
4,704 KB
testcase_21 AC 143 ms
5,280 KB
testcase_22 AC 153 ms
5,276 KB
testcase_23 AC 162 ms
5,124 KB
testcase_24 AC 172 ms
5,120 KB
testcase_25 AC 182 ms
5,084 KB
testcase_26 AC 192 ms
5,116 KB
testcase_27 AC 2 ms
4,376 KB
testcase_28 AC 2 ms
4,376 KB
testcase_29 AC 2 ms
4,376 KB
testcase_30 AC 58 ms
4,448 KB
testcase_31 AC 62 ms
4,376 KB
testcase_32 AC 2 ms
4,380 KB
testcase_33 AC 1 ms
4,380 KB
testcase_34 AC 2 ms
4,376 KB
testcase_35 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/*
k番目に小さい値を管理するデータ構造

できること
・要素の追加 O(1)
・要素の削除 O(1)
・k番目に小さい値の出力 O(1)




*/

#include <iostream>
#include <queue>
using namespace std;
using ll = long long;
using vll = vector<ll>;

struct DynamicPriorityQueue{
    priority_queue<ll> small;// small.top() は small の最大値 (K個になるようにする)
    priority_queue<ll, vector<ll>, greater<ll>> large;   // large.top() は large の最小値
    int K;

    void add(ll value) {
        if (small.size() < K)  small.push(value);
        else {
            ll current_kth = small.top();
            if (value < current_kth) {
                small.pop();
                small.push(value);
                large.push(current_kth);
            } else {
                large.push(value);
            }
        }
    }

    ll getKthSmallest() {
        if (small.size() < K) {
            return -1;  // K番目の値が存在しない場合は-1を返す
        }
        ll current_kth = small.top();
        return current_kth;
    }

    void remove() {
        if (small.size() < K) return;  // K番目の値が存在しない場合は何もしない
        ll current_kth = small.top();
        small.pop();
        if (!large.empty()) {
            long long next_kth = large.top();
            large.pop();
            small.push(next_kth);
        }
    }
};


int main() {
    DynamicPriorityQueue dq;
    int Q, K;
    cin >> Q >> K;
    dq.K = K;
    vll ans;

    for (int i = 0; i < Q; ++i) {
        int T;
        cin >> T;
        if (T == 1) {
            ll V;
            cin >> V;
            dq.add(V);
        } else {
            ll kthSmallest = dq.getKthSmallest();
            ans.push_back(kthSmallest);
            dq.remove();
        }
    }

    for (int i = 0; i < ans.size(); ++i) {
        cout << ans[i] << endl;
    }

    return 0;
}

0