結果

問題 No.318 学学学学学
ユーザー t8m8⛄️t8m8⛄️
提出日時 2017-06-10 17:53:10
言語 Nim
(2.0.2)
結果
AC  
実行時間 113 ms / 2,000 ms
コード長 3,201 bytes
コンパイル時間 3,709 ms
コンパイル使用メモリ 72,832 KB
実行使用メモリ 23,424 KB
最終ジャッジ日時 2024-06-30 01:20:15
合計ジャッジ時間 6,422 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
5,248 KB
testcase_01 AC 10 ms
6,144 KB
testcase_02 AC 12 ms
6,528 KB
testcase_03 AC 9 ms
5,888 KB
testcase_04 AC 11 ms
6,528 KB
testcase_05 AC 63 ms
23,424 KB
testcase_06 AC 59 ms
20,096 KB
testcase_07 AC 54 ms
18,688 KB
testcase_08 AC 46 ms
16,896 KB
testcase_09 AC 41 ms
15,744 KB
testcase_10 AC 36 ms
14,080 KB
testcase_11 AC 64 ms
23,296 KB
testcase_12 AC 48 ms
17,592 KB
testcase_13 AC 44 ms
16,512 KB
testcase_14 AC 42 ms
15,744 KB
testcase_15 AC 37 ms
14,208 KB
testcase_16 AC 35 ms
14,848 KB
testcase_17 AC 113 ms
19,328 KB
testcase_18 AC 43 ms
15,104 KB
testcase_19 AC 49 ms
19,328 KB
testcase_20 AC 28 ms
10,496 KB
testcase_21 AC 2 ms
5,376 KB
testcase_22 AC 2 ms
5,376 KB
testcase_23 AC 2 ms
5,376 KB
testcase_24 AC 2 ms
5,376 KB
testcase_25 AC 1 ms
5,376 KB
testcase_26 AC 2 ms
5,376 KB
testcase_27 AC 1 ms
5,376 KB
testcase_28 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
/home/judge/data/code/Main.nim(1, 39) Warning: Use the new 'sugar' module instead; future is deprecated [Deprecated]

ソースコード

diff #

import strutils, sequtils, algorithm, future, tables, sets

type
  PrirorityQueue*[T] = ref object of RootObj
    elms: seq[T]
    idxs: TableRef[T, int]
    cmp: (T, T) -> int

proc newPriorityQueue*[T](cmp: (T, T) -> int): PrirorityQueue[T] =
  new(result)
  result.elms = newSeq[T]()
  result.idxs = newTable[T, int]()
  result.cmp = cmp

template parent(cur: int): int =
  if cur == 0: -1
  else: (cur - 1) div 2

template childl(cur: int): int =
  cur * 2 + 1

template childr(cur: int): int =
  cur * 2 + 2

proc innerDel[T](self: PrirorityQueue[T], cur: int) =
  self.idxs.del(self.elms[cur])
  self.elms.del(cur)

proc innerSwap[T](self: PrirorityQueue[T], s, t: int) =
  let (sval, tval) = (self.elms[s], self.elms[t])
  swap(self.elms[s], self.elms[t])
  swap(self.idxs[sval], self.idxs[tval])

proc valid[T](self: PrirorityQueue[T], upper, lower: int): bool =
  let val = self.cmp(self.elms[upper], self.elms[lower])
  result = val < 0

proc valid[T](self: PrirorityQueue[T], cur: int = 0): bool =
  var (chl, chr) = (childl(cur), childr(cur))
  result = true
  if chl < self.len:
    result = result and self.valid(cur, chl)
    result = result and self.valid(chl)
  if chr < self.len:
    result = result and self.valid(cur, chr)
    result = result and self.valid(chr)

proc upHeap[T](self: PrirorityQueue[T], cur: var int) =
  var par = parent(cur)

  while par >= 0 and not self.valid(par, cur):
    self.innerSwap(cur, par)
    (cur, par) = (par, parent(par))

proc downHeap[T](self: PrirorityQueue[T], cur: var int) =
  var (chl, chr) = (childl(cur), childr(cur))

  while chl < self.elms.len:
    var ch =
      if chr >= self.elms.len or self.valid(chl, chr): chl
      else: chr

    if self.valid(cur, ch):
      break
    self.innerSwap(cur, ch)

    (cur, chl, chr) = (ch, childl(ch), childr(ch))

proc len*[T](self: PrirorityQueue[T]): int =
  self.elms.len

proc push*[T](self: PrirorityQueue[T], data: T): bool {.discardable.} =
  if self.idxs.contains(data):
    return false
  var
    n = self.elms.len
    cur = n
  self.elms.setLen(n + 1)
  self.elms[cur] = data
  self.idxs[data] = cur
  self.upHeap(cur)
  return true

proc peek*[T](self: PrirorityQueue[T]): T =
  self.elms[0]

proc pop*[T](self: PrirorityQueue[T]): T {.discardable.} =
  var
    n = self.elms.len
    cur = 0
  result = self.elms[0]
  self.innerSwap(0, n-1)
  self.innerDel(n-1)
  self.downHeap(cur)

proc del*[T](self: PrirorityQueue[T], data: T): bool {.discardable.} =
  if not self.idxs.contains(data):
    return false
  let n = self.elms.len
  var idx = self.idxs[data]
  if idx == n - 1:
    self.innerDel(n-1)
  else:
    self.innerSwap(idx, n-1)
    self.innerDel(n-1)
    self.downHeap(idx)
    self.upHeap(idx)
  return true

# ==============================================================================

when isMainModule:
  var
    n = stdin.readLine.parseInt
    a = stdin.readLine.split.map(parseInt)
    b = newSeq[int](n)
    lastIdx = newTable[int, int]()
    pQ = newPriorityQueue[int]((x: int, y: int) => y - x)

  for i in 0..<n:
    lastIdx[a[i]] = i

  for i in 0..<n:
    pQ.push(a[i])
    b[i] = pQ.peek()
    if i == lastIdx[a[i]]:
      pQ.del(a[i])

  echo b.join(" ")
0