結果

問題 No.649 ここでちょっとQK!
ユーザー vwxyz
提出日時 2022-03-29 13:27:57
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 321 ms / 3,000 ms
コード長 1,363 bytes
コンパイル時間 330 ms
コンパイル使用メモリ 82,048 KB
実行使用メモリ 84,996 KB
最終ジャッジ日時 2024-11-08 02:16:32
合計ジャッジ時間 7,756 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
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

class Slope_Trick:
    def __init__(self):
        self.queueL=[]
        self.queueR=[]
    def Push_Left(self,x):
        _heappush_max(self.queueL,heappushpop(self.queueR,x))
    def Push_Right(self,x):
        heappush(self.queueR,_heappushpop_max(self.queueL,x))
    def Pop_Left(self):
        return _heappop_max(self.queueL)
    def Pop_Right(self):
        return heappop(self.queueR)
    def __str__(self):
        return '['+', '.join(map(str,sorted(self.queueL)))+']+['+', '.join(map(str,sorted(self.queueR)))+']'


Q,K=map(int,readline().split())
ST=Slope_Trick()
for _ in range(Q):
    data=map(int,readline().split())
    q=next(data)
    if q==1:
        v=next(data)
        if len(ST.queueL)<K:
            ST.Push_Left(v)
        else:
            ST.Push_Right(v)
    else:
        if len(ST.queueL)==K:
            ans=ST.Pop_Left()
            if ST.queueR:
                ST.Push_Left(ST.Pop_Right())
        else:
            ans=-1
        print(ans)
0