結果

問題 No.318 学学学学学
ユーザー ikdikd
提出日時 2019-04-18 14:59:57
言語 Nim
(2.0.2)
結果
AC  
実行時間 86 ms / 2,000 ms
コード長 3,028 bytes
コンパイル時間 4,519 ms
コンパイル使用メモリ 69,856 KB
実行使用メモリ 28,320 KB
最終ジャッジ日時 2023-09-14 16:03:54
合計ジャッジ時間 6,809 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 7 ms
5,300 KB
testcase_01 AC 11 ms
7,028 KB
testcase_02 AC 13 ms
7,056 KB
testcase_03 AC 10 ms
6,680 KB
testcase_04 AC 12 ms
7,020 KB
testcase_05 AC 86 ms
28,320 KB
testcase_06 AC 77 ms
24,184 KB
testcase_07 AC 79 ms
23,572 KB
testcase_08 AC 72 ms
15,700 KB
testcase_09 AC 65 ms
17,688 KB
testcase_10 AC 54 ms
16,460 KB
testcase_11 AC 85 ms
26,572 KB
testcase_12 AC 70 ms
23,892 KB
testcase_13 AC 62 ms
20,604 KB
testcase_14 AC 57 ms
15,116 KB
testcase_15 AC 59 ms
15,908 KB
testcase_16 AC 58 ms
15,132 KB
testcase_17 AC 75 ms
25,080 KB
testcase_18 AC 57 ms
23,068 KB
testcase_19 AC 52 ms
23,976 KB
testcase_20 AC 48 ms
15,436 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 2 ms
4,376 KB
testcase_24 AC 1 ms
4,376 KB
testcase_25 AC 2 ms
4,376 KB
testcase_26 AC 1 ms
4,376 KB
testcase_27 AC 2 ms
4,380 KB
testcase_28 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

type HeapQueue*[T] = distinct seq[T]

proc newHeapQueue*[T](): HeapQueue[T] {.inline.} = HeapQueue[T](newSeq[T]())
proc newHeapQueue*[T](h: var HeapQueue[T]) {.inline.} = h = HeapQueue[T](
  newSeq[T]())

proc len*[T](h: HeapQueue[T]): int {.inline.} = seq[T](h).len
proc `[]`*[T](h: HeapQueue[T], i: int): T {.inline.} = seq[T](h)[i]
proc `[]=`[T](h: var HeapQueue[T], i: int,
  v: T) {.inline.} = seq[T](h)[i] = v
proc add[T](h: var HeapQueue[T], v: T) {.inline.} = seq[T](h).add(v)

proc heapCmp[T](x, y: T): bool {.inline.} =
  return (x < y)

# 'heap' is a heap at all indices >= startpos, except possibly for pos.  pos
# is the index of a leaf with a possibly out-of-order value.  Restore the
# heap invariant.
proc siftdown[T](heap: var HeapQueue[T], startpos, p: int) =
  var pos = p
  var newitem = heap[pos]
  # Follow the path to the root, moving parents down until finding a place
# newitem fits.
  while pos > startpos:
    let parentpos = (pos - 1) shr 1
    let parent = heap[parentpos]
    if heapCmp(newitem, parent):
      heap[pos] = parent
      pos = parentpos
    else:
      break
  heap[pos] = newitem

proc siftup[T](heap: var HeapQueue[T], p: int) =
  let endpos = len(heap)
  var pos = p
  let startpos = pos
  let newitem = heap[pos]
  # Bubble up the smaller child until hitting a leaf.
  var childpos = 2*pos + 1    # leftmost child position
  while childpos < endpos:
    # Set childpos to index of smaller child.
    let rightpos = childpos + 1
    if rightpos < endpos and not heapCmp(heap[childpos], heap[rightpos]):
      childpos = rightpos
    # Move the smaller child up.
    heap[pos] = heap[childpos]
    pos = childpos
    childpos = 2*pos + 1
  # The leaf at pos is empty now.  Put newitem there, and bubble it up
# to its final resting place (by sifting its parents down).
  heap[pos] = newitem
  siftdown(heap, startpos, pos)

proc push*[T](heap: var HeapQueue[T], item: T) =
  ## Push item onto heap, maintaining the heap invariant.
  (seq[T](heap)).add(item)
  siftdown(heap, 0, len(heap)-1)

proc pop*[T](heap: var HeapQueue[T]): T =
  ## Pop the smallest item off the heap, maintaining the heap invariant.
  let lastelt = seq[T](heap).pop()
  if heap.len > 0:
    result = heap[0]
    heap[0] = lastelt
    siftup(heap, 0)
  else:
    result = lastelt

# https://github.com/nim-lang/Nim/blob/master/lib/pure/collections/heapqueue.nim
# ---------------------------------------------------------------------------------------------- #
# ---------------------------------------------------------------------------------------------- #

import strutils, sequtils, tables

proc main() =
  let
    n = stdin.readLine.strip.parseInt
    a = stdin.readLine.strip.split.map(parseInt)
  var right = initTable[int, int]()
  for i in 0..<n:
    right[a[i]] = i
  var
    q = newHeapQueue[int]()
    b = newSeq[int](n)
  for i in 0..<n:
    while q.len > 0 and right[-q[0]] < i:
      discard q.pop
    q.push(-a[i])
    b[i] = -q[0]
  echo b.map(proc(it: int): string = $it).join(" ")
main()
0