結果

問題 No.649 ここでちょっとQK!
ユーザー kakira9618kakira9618
提出日時 2018-02-04 02:23:31
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 66 ms / 3,000 ms
コード長 1,101 bytes
コンパイル時間 453 ms
コンパイル使用メモリ 44,876 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-16 21:06:54
合計ジャッジ時間 2,992 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,816 KB
testcase_01 AC 1 ms
6,944 KB
testcase_02 AC 1 ms
6,944 KB
testcase_03 AC 35 ms
6,944 KB
testcase_04 AC 63 ms
6,944 KB
testcase_05 AC 53 ms
6,944 KB
testcase_06 AC 53 ms
6,944 KB
testcase_07 AC 2 ms
6,944 KB
testcase_08 AC 1 ms
6,944 KB
testcase_09 AC 1 ms
6,940 KB
testcase_10 AC 1 ms
6,940 KB
testcase_11 AC 1 ms
6,944 KB
testcase_12 AC 31 ms
6,944 KB
testcase_13 AC 29 ms
6,940 KB
testcase_14 AC 28 ms
6,944 KB
testcase_15 AC 30 ms
6,940 KB
testcase_16 AC 29 ms
6,940 KB
testcase_17 AC 34 ms
6,944 KB
testcase_18 AC 40 ms
6,944 KB
testcase_19 AC 42 ms
6,940 KB
testcase_20 AC 44 ms
6,940 KB
testcase_21 AC 49 ms
6,940 KB
testcase_22 AC 50 ms
6,944 KB
testcase_23 AC 57 ms
6,944 KB
testcase_24 AC 63 ms
6,940 KB
testcase_25 AC 62 ms
6,944 KB
testcase_26 AC 66 ms
6,940 KB
testcase_27 AC 2 ms
6,944 KB
testcase_28 AC 2 ms
6,940 KB
testcase_29 AC 1 ms
6,940 KB
testcase_30 AC 23 ms
6,940 KB
testcase_31 AC 25 ms
6,940 KB
testcase_32 AC 2 ms
6,940 KB
testcase_33 AC 1 ms
6,944 KB
testcase_34 AC 1 ms
6,940 KB
testcase_35 AC 1 ms
6,940 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:38:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   38 |     scanf("%d%d", &Q, &K);
      |     ~~~~~^~~~~~~~~~~~~~~~
main.cpp:42:21: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   42 |         int q; scanf("%d", &q);
      |                ~~~~~^~~~~~~~~~
main.cpp:44:35: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   44 |             long long int v; scanf("%lld", &v);
      |                              ~~~~~^~~~~~~~~~~~

ソースコード

diff #

#include <stdio.h>
#include <queue>

using namespace std;

template <class T>
struct KQue {
    priority_queue<T> max_q;
    priority_queue<T, vector<T>, greater<T>> min_q;
    size_t K;
    KQue(size_t k): K(k) {}
    void push(T v) {
        if (max_q.size() < K) {
            max_q.push(v); return;
        }
        T ma_v = max_q.top();
        if (ma_v > v) {
            max_q.pop();
            max_q.push(v);
            min_q.push(ma_v);
        } else {
            min_q.push(v);
        }
    }
    T get() {
        if (max_q.size() < K) return -1;
        T ma_v = max_q.top(); max_q.pop();
        if (min_q.size() != 0) {
            T mi_v = min_q.top(); min_q.pop();
            max_q.push(mi_v);
        }
        return ma_v;
    }
};

int main() {
    int Q, K;
    scanf("%d%d", &Q, &K);
    KQue<long long int> kque(K);
    
    for(int i = 0; i < Q; i++) {
        int q; scanf("%d", &q);
        if (q == 1) {
            long long int v; scanf("%lld", &v);
            kque.push(v);
        } else {
            printf("%lld\n", kque.get());
        }
    }
    return 0;
}
0