結果

問題 No.649 ここでちょっとQK!
ユーザー はむ吉🐹はむ吉🐹
提出日時 2018-02-11 17:19:36
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 1,151 ms / 3,000 ms
コード長 1,401 bytes
コンパイル時間 81 ms
コンパイル使用メモリ 12,672 KB
実行使用メモリ 20,224 KB
最終ジャッジ日時 2024-04-28 14:22:52
合計ジャッジ時間 16,388 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 31 ms
10,624 KB
testcase_01 AC 32 ms
10,624 KB
testcase_02 AC 31 ms
10,624 KB
testcase_03 AC 1,151 ms
10,752 KB
testcase_04 AC 779 ms
18,688 KB
testcase_05 AC 648 ms
18,688 KB
testcase_06 AC 637 ms
20,224 KB
testcase_07 AC 31 ms
10,624 KB
testcase_08 AC 30 ms
10,624 KB
testcase_09 AC 31 ms
10,752 KB
testcase_10 AC 30 ms
10,624 KB
testcase_11 AC 31 ms
10,624 KB
testcase_12 AC 393 ms
13,952 KB
testcase_13 AC 387 ms
13,952 KB
testcase_14 AC 379 ms
13,824 KB
testcase_15 AC 408 ms
13,952 KB
testcase_16 AC 380 ms
14,336 KB
testcase_17 AC 461 ms
14,464 KB
testcase_18 AC 500 ms
14,848 KB
testcase_19 AC 559 ms
15,104 KB
testcase_20 AC 603 ms
15,488 KB
testcase_21 AC 636 ms
15,744 KB
testcase_22 AC 693 ms
16,000 KB
testcase_23 AC 737 ms
16,384 KB
testcase_24 AC 756 ms
16,768 KB
testcase_25 AC 812 ms
17,024 KB
testcase_26 AC 856 ms
17,280 KB
testcase_27 AC 35 ms
10,752 KB
testcase_28 AC 35 ms
10,752 KB
testcase_29 AC 34 ms
10,880 KB
testcase_30 AC 387 ms
13,952 KB
testcase_31 AC 409 ms
14,208 KB
testcase_32 AC 31 ms
10,624 KB
testcase_33 AC 30 ms
10,624 KB
testcase_34 AC 31 ms
10,624 KB
testcase_35 AC 31 ms
10,624 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/env python3

import heapq

INVALID = -1


class Solver(object):

    def __init__(self, k):
        self.k = k
        # 最小からK個までの要素を格納する最大ヒープ
        # 実装上は最小ヒープ+負数で代用
        self.max_heap = []
        # 残りの要素を格納する最小ヒープ
        self.min_heap = []

    def parse_query1(self, v):
        if len(self.max_heap) < self.k:
            heapq.heappush(self.max_heap, -v)
        else:
            m1 = -self.max_heap[0]
            if m1 <= v:
                heapq.heappush(self.min_heap, v)
            else:
                _ = heapq.heappop(self.max_heap)
                heapq.heappush(self.min_heap, m1)
                heapq.heappush(self.max_heap, -v)

    def parse_query2(self):
        if len(self.max_heap) < self.k:
            return INVALID
        else:
            res = -heapq.heappop(self.max_heap)
            if self.min_heap:
                heapq.heappush(self.max_heap,
                               -heapq.heappop(self.min_heap))
            return res


def main():
    q, k = (int(z) for z in input().split())
    solver = Solver(k)
    for _ in range(q):
        args = [int(z) for z in input().split()]
        if args[0] == 1:
            solver.parse_query1(args[1])
        else:
            print(solver.parse_query2())


if __name__ == '__main__':
    main()
0