結果

問題 No.649 ここでちょっとQK!
ユーザー HanahanaHanahana
提出日時 2023-06-27 21:57:20
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 287 ms / 3,000 ms
コード長 2,927 bytes
コンパイル時間 704 ms
コンパイル使用メモリ 78,260 KB
実行使用メモリ 12,712 KB
最終ジャッジ日時 2023-09-17 20:17:22
合計ジャッジ時間 8,578 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 287 ms
5,156 KB
testcase_04 AC 242 ms
12,640 KB
testcase_05 AC 209 ms
12,712 KB
testcase_06 AC 214 ms
12,712 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 120 ms
7,172 KB
testcase_13 AC 120 ms
7,176 KB
testcase_14 AC 117 ms
7,144 KB
testcase_15 AC 123 ms
7,128 KB
testcase_16 AC 122 ms
7,556 KB
testcase_17 AC 139 ms
7,712 KB
testcase_18 AC 155 ms
8,356 KB
testcase_19 AC 169 ms
8,412 KB
testcase_20 AC 183 ms
9,056 KB
testcase_21 AC 196 ms
9,372 KB
testcase_22 AC 211 ms
9,640 KB
testcase_23 AC 228 ms
10,068 KB
testcase_24 AC 242 ms
10,340 KB
testcase_25 AC 259 ms
10,768 KB
testcase_26 AC 277 ms
11,040 KB
testcase_27 AC 2 ms
4,376 KB
testcase_28 AC 2 ms
4,380 KB
testcase_29 AC 2 ms
4,376 KB
testcase_30 AC 86 ms
7,096 KB
testcase_31 AC 90 ms
7,332 KB
testcase_32 AC 2 ms
4,380 KB
testcase_33 AC 2 ms
4,380 KB
testcase_34 AC 1 ms
4,380 KB
testcase_35 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/*

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

できること
・要素の追加 O(logN) dq.add(x)
・k番目に小さい要素の削除 O(1) dq.remove()
・任意の値を探して削除 0(logN)
・k番目に小さい値の出力 O(1)
    そもそもk個も存在しない場合 : -1を出力

k番目に大きい値であれば全部にマイナスをつければ良い

コードチェックは https://yukicoder.me/problems/no/649

*/

#include <iostream>
#include <set>
#include <vector>
#include <algorithm>

using namespace std;
using ll = long long;
using vll = vector<ll>;

struct DynamicMultiset {
    multiset<ll> small;   // smallは昇順で要素を保持するmultiset
    multiset<ll> large;   // largeは昇順で要素を保持するmultiset
    int K;                // k番目に小さい値の管理数

    void add(ll value) {
        if (small.size() < K) {
            small.insert(value);
        } else {
            ll current_kth = *(--small.end());
            if (value < current_kth) {
                small.erase(--small.end());
                small.insert(value);
                large.insert(current_kth);
            } else {
                large.insert(value);
            }
        }
    }

    ll getKthSmallest() {
        if (small.size() < K) {
            return -1;
        }
        return *(--small.end());
    }

    void remove() {
        if (small.size() < K) {
            return;
        }
        ll current_kth = *(--small.end());
        small.erase(--small.end());
        if (!large.empty()) {
            ll next_kth = *large.begin();
            large.erase(large.begin());
            small.insert(next_kth);
        }
    }

    void remove(ll value) {
        auto ite_small = find(small.begin(), small.end(), value);
        auto ite_large = find(large.begin(), large.end(), value);
        if(ite_small != small.end()) {
            small.erase(ite_small);
            if (!large.empty()) {
                ll next_kth = *large.begin();
                large.erase(large.begin());
                small.insert(next_kth);
            }
        }
        else if(ite_large != large.end()) {
            large.erase(ite_large);
        }
        else return; //どこにもなかったら何もしない
    }

    multiset<ll> k_small_set(){
        return small;
    }
};



using ll = long long;
using vll = vector<long long>;

int main() {
    DynamicMultiset dm;
    int Q, K;
    cin >> Q >> K;
    dm.K = K;
    vll ans;

    for (int i = 0; i < Q; ++i) {
        int T;
        cin >> T;

        if (T == 1) {
            ll V;
            cin >> V;
            dm.add(V);
        } else {
            ll kthSmallest = dm.getKthSmallest();
            ans.push_back(kthSmallest);
            dm.remove();
        }
    }


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

    return 0;
}
0