結果

問題 No.649 ここでちょっとQK!
ユーザー kakira9618kakira9618
提出日時 2018-02-04 02:38:53
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 74 ms / 3,000 ms
コード長 1,101 bytes
コンパイル時間 434 ms
コンパイル使用メモリ 45,008 KB
実行使用メモリ 5,604 KB
最終ジャッジ日時 2024-04-16 21:07:13
合計ジャッジ時間 3,017 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 38 ms
5,376 KB
testcase_04 AC 71 ms
5,604 KB
testcase_05 AC 60 ms
5,472 KB
testcase_06 AC 60 ms
5,376 KB
testcase_07 AC 1 ms
5,376 KB
testcase_08 AC 1 ms
5,376 KB
testcase_09 AC 1 ms
5,376 KB
testcase_10 AC 1 ms
5,376 KB
testcase_11 AC 1 ms
5,376 KB
testcase_12 AC 33 ms
5,376 KB
testcase_13 AC 33 ms
5,376 KB
testcase_14 AC 32 ms
5,376 KB
testcase_15 AC 35 ms
5,376 KB
testcase_16 AC 35 ms
5,376 KB
testcase_17 AC 41 ms
5,376 KB
testcase_18 AC 45 ms
5,376 KB
testcase_19 AC 49 ms
5,376 KB
testcase_20 AC 52 ms
5,376 KB
testcase_21 AC 55 ms
5,376 KB
testcase_22 AC 60 ms
5,376 KB
testcase_23 AC 64 ms
5,376 KB
testcase_24 AC 68 ms
5,376 KB
testcase_25 AC 71 ms
5,376 KB
testcase_26 AC 74 ms
5,376 KB
testcase_27 AC 2 ms
5,376 KB
testcase_28 AC 2 ms
5,376 KB
testcase_29 AC 2 ms
5,376 KB
testcase_30 AC 26 ms
5,376 KB
testcase_31 AC 30 ms
5,376 KB
testcase_32 AC 2 ms
5,376 KB
testcase_33 AC 1 ms
5,376 KB
testcase_34 AC 2 ms
5,376 KB
testcase_35 AC 1 ms
5,376 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 QueK {
    priority_queue<T> max_q;
    priority_queue<T, vector<T>, greater<T>> min_q;
    size_t K;
    QueK(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);
    QueK<long long int> quek(K);
    
    for(int i = 0; i < Q; i++) {
        int q; scanf("%d", &q);
        if (q == 1) {
            long long int v; scanf("%lld", &v);
            quek.push(v);
        } else {
            printf("%lld\n", quek.get());
        }
    }
    return 0;
}
0