結果

問題 No.59 鉄道の旅
ユーザー むらためむらため
提出日時 2019-02-04 08:56:47
言語 Nim
(2.0.2)
結果
AC  
実行時間 19 ms / 5,000 ms
コード長 2,031 bytes
コンパイル時間 2,656 ms
コンパイル使用メモリ 66,176 KB
実行使用メモリ 10,968 KB
最終ジャッジ日時 2023-09-14 03:27:35
合計ジャッジ時間 3,805 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
10,904 KB
testcase_01 AC 5 ms
10,812 KB
testcase_02 AC 4 ms
10,772 KB
testcase_03 AC 4 ms
10,828 KB
testcase_04 AC 19 ms
10,968 KB
testcase_05 AC 5 ms
10,772 KB
testcase_06 AC 5 ms
10,832 KB
testcase_07 AC 5 ms
10,776 KB
testcase_08 AC 6 ms
10,816 KB
testcase_09 AC 6 ms
10,708 KB
testcase_10 AC 6 ms
10,764 KB
testcase_11 AC 5 ms
10,804 KB
testcase_12 AC 5 ms
10,820 KB
testcase_13 AC 12 ms
10,740 KB
testcase_14 AC 11 ms
10,820 KB
testcase_15 AC 4 ms
10,796 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
/home/judge/data/code/Main.nim(1, 8) Warning: imported and not used: 'sequtils' [UnusedImport]
/home/judge/data/code/Main.nim(1, 41) Warning: imported and not used: 'sugar' [UnusedImport]
/home/judge/data/code/Main.nim(1, 36) Warning: imported and not used: 'math' [UnusedImport]

ソースコード

diff #

import sequtils,strutils,algorithm,math,sugar,macros
template get*():string = stdin.readLine().strip()
macro unpack*(arr: auto,cnt: static[int]): auto =
  let t = genSym(); result = quote do:(let `t` = `arr`;())
  for i in 0..<cnt: result[0][1].add(quote do:`t`[`i`])
template times*(n:int,body:untyped): untyped = (for _ in 0..<n: body)
template `max=`*(x,y:typed):void = x = max(x,y)
template `min=`*(x,y:typed):void = x = min(x,y)

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()
template useScan =
  proc getchar_unlocked():char {. importc:"getchar_unlocked",header: "<stdio.h>" .}
  proc scan(): int =
    result = 0
    var minus = false
    while true:
      var k = getchar_unlocked()
      if k == '-' : minus = true
      elif k < '0' or k > '9': break
      else: result = 10 * result + k.ord - '0'.ord
    if minus: result *= -1

useScan()

var last = 0
let n = scan()
let k = scan()
var cargo =  initBinaryIndexedTree[int](100_0001)
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