結果

問題 No.649 ここでちょっとQK!
ユーザー むらためむらため
提出日時 2019-10-12 15:50:54
言語 Nim
(2.0.0)
結果
AC  
実行時間 40 ms / 3,000 ms
コード長 2,763 bytes
コンパイル時間 3,394 ms
コンパイル使用メモリ 67,356 KB
実行使用メモリ 8,956 KB
最終ジャッジ日時 2023-08-18 18:30:10
合計ジャッジ時間 8,075 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 8 ms
4,376 KB
testcase_04 AC 40 ms
7,212 KB
testcase_05 AC 20 ms
8,956 KB
testcase_06 AC 18 ms
8,356 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 14 ms
5,000 KB
testcase_13 AC 14 ms
4,916 KB
testcase_14 AC 13 ms
4,912 KB
testcase_15 AC 16 ms
5,128 KB
testcase_16 AC 14 ms
5,252 KB
testcase_17 AC 19 ms
5,284 KB
testcase_18 AC 22 ms
5,304 KB
testcase_19 AC 24 ms
5,308 KB
testcase_20 AC 26 ms
5,276 KB
testcase_21 AC 29 ms
6,068 KB
testcase_22 AC 31 ms
6,076 KB
testcase_23 AC 33 ms
6,056 KB
testcase_24 AC 34 ms
6,080 KB
testcase_25 AC 36 ms
7,196 KB
testcase_26 AC 38 ms
7,208 KB
testcase_27 AC 1 ms
4,376 KB
testcase_28 AC 1 ms
4,384 KB
testcase_29 AC 2 ms
4,380 KB
testcase_30 AC 11 ms
5,024 KB
testcase_31 AC 15 ms
4,764 KB
testcase_32 AC 2 ms
4,380 KB
testcase_33 AC 2 ms
4,376 KB
testcase_34 AC 2 ms
4,380 KB
testcase_35 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
/home/judge/data/code/Main.nim(2, 8) Warning: imported and not used: 'sequtils' [UnusedImport]

ソースコード

diff #

{.checks:off.}
import sequtils
template times*(n:int,body) = (for _ in 0..<n: body)
proc printf(formatstr: cstring){.header: "<stdio.h>", varargs.}
proc getchar_unlocked():char {. importc:"getchar_unlocked",header: "<stdio.h>" .}
proc scan(): int =
  while true:
    let k = getchar_unlocked()
    if k < '0': return
    result = 10 * result + k.ord - '0'.ord

template useBinaryHeap() =
  type
    BinaryHeap*[T] = object
      nodes: seq[T]
      compare: proc(x,y:T):int
      popchunk: bool
  proc newBinaryHeap*[T](compare:proc(x,y:T):int): BinaryHeap[T] =
    BinaryHeap[T](nodes:newSeq[T](),compare:compare,popchunk:false)
  proc compareNode[T](h:BinaryHeap[T],i,j:int):int = h.compare(h.nodes[i],h.nodes[j])
  proc size*[T](h:BinaryHeap[T]):int = h.nodes.len() - h.popchunk.int
  proc items*[T](h:var BinaryHeap[T]):seq[T] =
    if h.popchunk : discard h.popimpl()
    return h.nodes
  proc top*[T](h:var BinaryHeap[T]): T =
    if h.popchunk : discard h.popimpl()
    return h.nodes[0]
  proc push*[T](h:var BinaryHeap[T],node:T):void =
    if h.popchunk :
      h.nodes[0] = node
      h.shiftdown()
    else: h.pushimpl(node)
  proc pop*[T](h:var BinaryHeap[T]):T =
    if h.popchunk: discard h.popimpl()
    h.popchunk = true
    return h.nodes[0]

  proc shiftdown[T](h:var BinaryHeap[T]): void =
    h.popchunk = false
    let size = h.nodes.len()
    var i = 0
    while true :
      let L = i * 2 + 1
      let R = i * 2 + 2
      if L >= size : break
      let child = if R < size and h.compareNode(R,L) <= 0 : R else: L
      if h.compareNode(i,child) <= 0: break
      swap(h.nodes[i],h.nodes[child])
      i = child

  proc pushimpl[T](h:var BinaryHeap[T],node:T):void =
    h.nodes.add(node) #末尾に追加
    var i = h.nodes.len() - 1
    while i > 0: # 末尾から木を整形
      let parent = (i - 1) div 2
      if h.compare(h.nodes[parent],node) <= 0: break
      h.nodes[i] = h.nodes[parent]
      i = parent
    h.nodes[i] = node

  proc popimpl[T](h:var BinaryHeap[T]):T =
    result = h.nodes[0] # rootと末尾を入れ替えて木を整形
    h.nodes[0] = h.nodes[^1]
    h.nodes.setLen(h.nodes.len() - 1)
    h.shiftdown()

useBinaryHeap()

let q = scan()
let k = scan()
var less = newBinaryHeap[int](proc(x,y:int):int = -x+y)
var greater = newBinaryHeap[int](proc(x,y:int):int = -y+x)
q.times:
  if less.size() > 0 : discard less.top()
  if greater.size() > 0 : discard greater.top()
  if scan() == 1:
    let v = scan()
    if less.size() < k : less.push(v)
    else:
      if v < less.top() :
        greater.push(less.pop())
        less.push(v)
      else: greater.push(v)
  else:
    if less.size() < k : printf("-1\n")
    else:
      printf("%ld\n",less.pop())
      if greater.size() > 0 : less.push(greater.pop())
0