結果

問題 No.59 鉄道の旅
ユーザー むらためむらため
提出日時 2019-02-04 08:50:54
言語 Nim
(2.0.2)
結果
WA  
実行時間 -
コード長 1,517 bytes
コンパイル時間 2,247 ms
コンパイル使用メモリ 66,592 KB
実行使用メモリ 4,500 KB
最終ジャッジ日時 2023-09-14 03:27:39
合計ジャッジ時間 3,106 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 WA -
testcase_02 RE -
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 3 ms
4,376 KB
testcase_13 RE -
testcase_14 RE -
testcase_15 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
/home/judge/data/code/Main.nim(1, 8) Warning: imported and not used: 'sequtils' [UnusedImport]

ソースコード

diff #

import sequtils
template times*(n:int,body:untyped): untyped = (for _ in 0..<n: body)
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 useBinaryIndexedTree() = # 部分和検索 / 更新 O(log(N))
  type BinaryIndexedTree[T] = ref object
    data: seq[T] # 引数以下の部分和(Fenwick Tree)
  proc initBinaryIndexedTree[T](n:int):BinaryIndexedTree[T] =
    new(result)
    result.data = newSeq[T](n)
  proc len[T](self:BinaryIndexedTree[T]): int = self.data.len()
  proc update[T](self:var BinaryIndexedTree[T],i:int,val:T) =
    var i = i
    while i < self.len():
      self.data[i] += val
      i = i or (i + 1)
  proc `[]`[T](self:BinaryIndexedTree[T],i:int): T =
    var i = i
    while i >= 0:
      result += self.data[i]
      i = (i and (i + 1)) - 1
  proc `$`[T](self:BinaryIndexedTree[T]): string =
    result = "["
    for i in 0..<self.len.min(100): result &= $(self[i]) & ", "
    return result[0..result.len()-2] & (if self.len > 100 : " ...]" else: "]")
useBinaryIndexedTree()
let n = scan()
let k = scan()
var cargo = initBinaryIndexedTree[int](n)
var last = 0
n.times:
  let w = scan()
  if w > 0:
    if last - cargo[w-1] >= k: continue # W以上の荷物がK個以上
    cargo.update w, 1
    last += 1
  else:
    if cargo[-w-1] == cargo[-w] : continue # 荷物が無い
    cargo.update -w,-1
    last -= 1
echo last
0