結果

問題 No.649 ここでちょっとQK!
ユーザー HanahanaHanahana
提出日時 2023-06-27 18:33:31
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 298 ms / 3,000 ms
コード長 1,783 bytes
コンパイル時間 648 ms
コンパイル使用メモリ 76,872 KB
実行使用メモリ 12,988 KB
最終ジャッジ日時 2023-09-17 16:14:36
合計ジャッジ時間 6,498 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 298 ms
5,036 KB
testcase_04 AC 244 ms
12,708 KB
testcase_05 AC 211 ms
12,988 KB
testcase_06 AC 216 ms
12,884 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 122 ms
7,064 KB
testcase_13 AC 120 ms
7,096 KB
testcase_14 AC 118 ms
7,024 KB
testcase_15 AC 124 ms
7,136 KB
testcase_16 AC 120 ms
7,456 KB
testcase_17 AC 135 ms
7,868 KB
testcase_18 AC 151 ms
8,192 KB
testcase_19 AC 167 ms
8,420 KB
testcase_20 AC 188 ms
8,880 KB
testcase_21 AC 204 ms
9,312 KB
testcase_22 AC 218 ms
9,676 KB
testcase_23 AC 243 ms
9,944 KB
testcase_24 AC 253 ms
10,340 KB
testcase_25 AC 256 ms
10,760 KB
testcase_26 AC 277 ms
11,148 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 84 ms
7,044 KB
testcase_31 AC 92 ms
7,364 KB
testcase_32 AC 2 ms
4,376 KB
testcase_33 AC 1 ms
4,376 KB
testcase_34 AC 2 ms
4,376 KB
testcase_35 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

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



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