結果

問題 No.3 ビットすごろく
ユーザー n_knuun_knuu
提出日時 2017-01-04 20:05:13
言語 Nim
(2.0.2)
結果
AC  
実行時間 3 ms / 5,000 ms
コード長 1,969 bytes
コンパイル時間 3,243 ms
コンパイル使用メモリ 69,872 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-14 00:18:57
合計ジャッジ時間 4,317 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 3 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,384 KB
testcase_09 AC 3 ms
4,376 KB
testcase_10 AC 3 ms
4,376 KB
testcase_11 AC 3 ms
4,380 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 3 ms
4,376 KB
testcase_16 AC 3 ms
4,376 KB
testcase_17 AC 3 ms
4,380 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 3 ms
4,380 KB
testcase_20 AC 2 ms
4,384 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 3 ms
4,380 KB
testcase_23 AC 3 ms
4,376 KB
testcase_24 AC 3 ms
4,376 KB
testcase_25 AC 3 ms
4,376 KB
testcase_26 AC 1 ms
4,376 KB
testcase_27 AC 2 ms
4,376 KB
testcase_28 AC 3 ms
4,380 KB
testcase_29 AC 3 ms
4,380 KB
testcase_30 AC 1 ms
4,380 KB
testcase_31 AC 1 ms
4,376 KB
testcase_32 AC 3 ms
4,376 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
/home/judge/data/code/Main.nim(7, 20) Warning: inherit from a more precise exception type like ValueError, IOError or OSError. If these don't suit, inherit from CatchableError or Defect. [InheritFromException]

ソースコード

diff #

import strutils, sequtils
type
  Heap[T]= object
    data: seq[T]
    size: int
    comp: proc (x: T, y: T): int
  EmptyHeapError = object of Exception

proc siftup[T](h: var Heap[T]) =
  var i = h.size - 1
  while i > 0:
    var par = (i-1) div 2
    if h.comp(h.data[par], h.data[i]) > 0:
      swap(h.data[i], h.data[par])
      i = par
    else:
      break
proc siftdown[T](h: var Heap[T]) =
  var par = 0
  while par < h.size:
    let (left, right) = (2*par+1, 2*par+2)
    if right < h.size:
      if h.comp(h.data[par], min(h.data[left], h.data[right])) <= 0:
        break
      elif h.comp(h.data[left], h.data[right]) < 0:
        swap(h.data[par], h.data[left])
        par = left
      else:
        swap(h.data[par], h.data[right])
        par = right
    elif left < h.size:
      if h.comp(h.data[left], h.data[par]) < 0:
        swap(h.data[par], h.data[left])
        par = left
      else:
        break
    else:
      break
proc initHeap[T](comparator: proc (x: T, y: T): int = cmp): Heap[T] =
  Heap[T](data: newSeq[T](), size: 0, comp: comparator)
proc push[T](h: var Heap[T], x: T) =
  h.data.add(x)
  h.size.inc
  h.siftup
proc pop[T](h: var Heap[T]): T =
  if h.size <= 0:
    raise newException(EmptyHeapError, "cannot pop element, heap is empty")
  result = h.data[0]
  h.data[0] = h.data[^1]
  h.size.dec
  h.data.setlen(h.size)
  h.siftdown
proc empty[T](h: var Heap[T]): bool {. inline .} = h.data.len == 0

proc popcount(x: int): cint {.
  importc: "__builtin_popcount", nodecl, nosideeffect.}

let N = stdin.readline.parseInt
var
  dist = newSeqWith(N+1, 1e9.int)
  pque = initHeap[(int, int)]()
pque.push((1, 1))
while not pque.empty:
  let (c, v) = pque.pop
  if dist[v] <= c:
    continue
  dist[v] = c
  if v == N:
    echo(c)
    quit()
  let pcnt = popcount(v)
  for sgn in @[-1, 1]:
    if 0 < v + sgn * pcnt and v + sgn * pcnt <= N and dist[v] + 1 < dist[v + sgn * pcnt]:
      pque.push((dist[v] + 1, v + sgn * pcnt))
echo(-1)
0