結果

問題 No.649 ここでちょっとQK!
ユーザー HanahanaHanahana
提出日時 2023-06-27 17:50:57
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,107 bytes
コンパイル時間 852 ms
コンパイル使用メモリ 90,356 KB
実行使用メモリ 14,180 KB
最終ジャッジ日時 2023-09-17 15:22:40
合計ジャッジ時間 6,378 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 1 ms
4,380 KB
testcase_02 WA -
testcase_03 AC 290 ms
5,192 KB
testcase_04 AC 202 ms
14,180 KB
testcase_05 WA -
testcase_06 AC 168 ms
5,272 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 AC 2 ms
4,380 KB
testcase_34 AC 1 ms
4,376 KB
testcase_35 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <queue>
#include <unordered_map>
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 の最小値
    unordered_map<ll, int> elementCounts;                         // 要素の出現回数を管理するハッシュマップ
    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);
            }
        }
        elementCounts[value]++;
    }

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

    void remove(ll x) {
        if (elementCounts.find(x) != elementCounts.end()) {
            elementCounts[x]--;
            if (elementCounts[x] == 0) {
                elementCounts.erase(x);
            }
        }

        // largeにxが存在する場合は、smallの最小値と交換する
        if (!large.empty() && large.top() == x) {
            large.pop();
            small.push(x);
        }
    }
};


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(kthSmallest);
        }
    }

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

    return 0;
}
0