結果

問題 No.649 ここでちょっとQK!
ユーザー vwxyz
提出日時 2022-03-19 19:15:27
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 457 ms / 3,000 ms
コード長 1,022 bytes
コンパイル時間 329 ms
コンパイル使用メモリ 82,440 KB
実行使用メモリ 89,156 KB
最終ジャッジ日時 2024-10-05 02:10:45
合計ジャッジ時間 9,081 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 32
権限があれば一括ダウンロードができます

ソースコード

diff #

import heapq
import sys
from heapq import heappush,heappop,heapify,heappushpop,_heappop_max,_heapify_max
def _heappush_max(heap,item):
    heap.append(item)
    heapq._siftdown_max(heap, 0, len(heap)-1)
def _heappushpop_max(heap, item):
    if heap and item < heap[0]:
        item, heap[0] = heap[0], item
        heapq._siftup_max(heap, 0)
    return item
readline=sys.stdin.readline

Q,K=map(int,readline().split())
queue0,queue1=[],[]
for _ in range(Q):
    data=map(int,readline().split())
    if next(data)==1:
        v=next(data)
        if len(queue0)<K:
            _heappush_max(queue0,v)
        else:
            heappush(queue1,v)
            x=_heappop_max(queue0)
            y=heappop(queue1)
            if x>y:
                x,y=y,x
            _heappush_max(queue0,x)
            heappush(queue1,y)
    else:
        if len(queue0)<K:
            ans=-1
        else:
            ans=_heappop_max(queue0)
            if queue1:
                _heappush_max(queue0,heappop(queue1))
        print(ans)
0