結果

問題 No.649 ここでちょっとQK!
ユーザー むらためむらため
提出日時 2019-01-30 04:31:14
言語 Nim
(2.0.2)
結果
CE  
(最新)
AC  
(最初)
実行時間 -
コード長 4,132 bytes
コンパイル時間 377 ms
コンパイル使用メモリ 42,096 KB
最終ジャッジ日時 2024-04-27 02:48:20
合計ジャッジ時間 1,375 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
コンパイルエラー時のメッセージ・ソースコードは、提出者また管理者しか表示できないようにしております。(リジャッジ後のコンパイルエラーは公開されます)
ただし、clay言語の場合は開発者のデバッグのため、公開されます。

コンパイルメッセージ
/home/judge/data/code/Main.nim(3, 16) Error: 'untyped' is only allowed in templates and macros or magic procs

ソースコード

diff #

import sequtils,macros
template times*(n:int,body) = (for _ in 0..<n: body)
proc puts(str: untyped){.header: "<stdio.h>", varargs.}
proc printf(formatstr: cstring){.header: "<stdio.h>", varargs.}
proc pc(c:char){. importc:"putchar_unlocked",header: "<stdio.h>" .}
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

proc printInt(a0:int) =
  template put(n:int) = pc("0123456789"[n])
  proc getPrintIntNimCode(n,maxA:static[int64]):string =
    result = "if a0 < " & $maxA & ":\n"
    for i in 1..n: result &= "  let a" & $i & " = a" & $(i-1) & " div 10\n"
    result &= "  put(a" & $n & ")\n"
    for i in n.countdown(1): result &= "  put(a" & $(i-1) & "-a" & $i & "*10)\n"
    result &= "  return"
  macro eval(s:static[string]): auto = parseStmt(s)
  eval(getPrintIntNimCode(0,10))
  eval(getPrintIntNimCode(1,100))
  eval(getPrintIntNimCode(2,1000))
  eval(getPrintIntNimCode(3,10000))
  eval(getPrintIntNimCode(4,100000))
  eval(getPrintIntNimCode(5,1000000))
  eval(getPrintIntNimCode(6,10000000))
  eval(getPrintIntNimCode(7,100000000))
  eval(getPrintIntNimCode(8,1000000000))
  eval(getPrintIntNimCode(9,10000000000))
  eval(getPrintIntNimCode(10,100000000000))
  eval(getPrintIntNimCode(11,1000000000000))
  eval(getPrintIntNimCode(12,10000000000000))
  eval(getPrintIntNimCode(13,100000000000000))
  eval(getPrintIntNimCode(14,1000000000000000))
  eval(getPrintIntNimCode(15,10000000000000000))
  eval(getPrintIntNimCode(16,100000000000000000))
  eval(getPrintIntNimCode(17,1000000000000000000))
  eval(getPrintIntNimCode(18,9000000000000000000))


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 : pc('-');pc('1')
    else:
      less.pop().printInt()
      if greater.size() > 0 : less.push(greater.pop())
    pc('\n')
0