結果

問題 No.738 平らな農地
ユーザー ikdikd
提出日時 2019-04-18 12:43:12
言語 Nim
(2.0.2)
結果
AC  
実行時間 650 ms / 2,000 ms
コード長 3,133 bytes
コンパイル時間 3,248 ms
コンパイル使用メモリ 69,140 KB
実行使用メモリ 14,588 KB
最終ジャッジ日時 2023-09-14 16:05:37
合計ジャッジ時間 31,149 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 9 ms
4,380 KB
testcase_06 AC 11 ms
4,380 KB
testcase_07 AC 12 ms
4,376 KB
testcase_08 AC 6 ms
4,376 KB
testcase_09 AC 3 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 6 ms
4,380 KB
testcase_12 AC 4 ms
4,380 KB
testcase_13 AC 9 ms
4,376 KB
testcase_14 AC 4 ms
4,376 KB
testcase_15 AC 566 ms
12,972 KB
testcase_16 AC 578 ms
12,988 KB
testcase_17 AC 610 ms
12,864 KB
testcase_18 AC 599 ms
12,808 KB
testcase_19 AC 415 ms
13,116 KB
testcase_20 AC 580 ms
12,872 KB
testcase_21 AC 650 ms
13,376 KB
testcase_22 AC 602 ms
13,084 KB
testcase_23 AC 617 ms
13,104 KB
testcase_24 AC 534 ms
12,980 KB
testcase_25 AC 4 ms
4,380 KB
testcase_26 AC 4 ms
4,380 KB
testcase_27 AC 4 ms
4,376 KB
testcase_28 AC 4 ms
4,376 KB
testcase_29 AC 5 ms
4,376 KB
testcase_30 AC 4 ms
4,376 KB
testcase_31 AC 4 ms
4,376 KB
testcase_32 AC 5 ms
4,380 KB
testcase_33 AC 4 ms
4,376 KB
testcase_34 AC 4 ms
4,376 KB
testcase_35 AC 4 ms
4,376 KB
testcase_36 AC 4 ms
4,380 KB
testcase_37 AC 4 ms
4,380 KB
testcase_38 AC 4 ms
4,376 KB
testcase_39 AC 5 ms
4,380 KB
testcase_40 AC 4 ms
4,376 KB
testcase_41 AC 4 ms
4,380 KB
testcase_42 AC 5 ms
4,376 KB
testcase_43 AC 5 ms
4,380 KB
testcase_44 AC 5 ms
4,376 KB
testcase_45 AC 572 ms
14,520 KB
testcase_46 AC 555 ms
12,996 KB
testcase_47 AC 560 ms
13,096 KB
testcase_48 AC 500 ms
12,936 KB
testcase_49 AC 494 ms
13,040 KB
testcase_50 AC 506 ms
13,200 KB
testcase_51 AC 588 ms
12,804 KB
testcase_52 AC 532 ms
12,916 KB
testcase_53 AC 552 ms
12,976 KB
testcase_54 AC 580 ms
12,820 KB
testcase_55 AC 596 ms
12,800 KB
testcase_56 AC 577 ms
13,180 KB
testcase_57 AC 529 ms
13,004 KB
testcase_58 AC 549 ms
13,012 KB
testcase_59 AC 558 ms
14,524 KB
testcase_60 AC 533 ms
14,484 KB
testcase_61 AC 538 ms
13,636 KB
testcase_62 AC 520 ms
12,860 KB
testcase_63 AC 608 ms
14,576 KB
testcase_64 AC 574 ms
13,492 KB
testcase_65 AC 68 ms
12,828 KB
testcase_66 AC 70 ms
12,608 KB
testcase_67 AC 275 ms
12,796 KB
testcase_68 AC 280 ms
12,332 KB
testcase_69 AC 332 ms
12,400 KB
testcase_70 AC 295 ms
14,484 KB
testcase_71 AC 304 ms
12,760 KB
testcase_72 AC 251 ms
12,660 KB
testcase_73 AC 241 ms
12,896 KB
testcase_74 AC 423 ms
12,516 KB
testcase_75 AC 351 ms
12,768 KB
testcase_76 AC 343 ms
12,412 KB
testcase_77 AC 287 ms
12,688 KB
testcase_78 AC 311 ms
13,456 KB
testcase_79 AC 484 ms
14,300 KB
testcase_80 AC 440 ms
12,356 KB
testcase_81 AC 442 ms
12,828 KB
testcase_82 AC 407 ms
12,444 KB
testcase_83 AC 344 ms
14,588 KB
testcase_84 AC 505 ms
13,568 KB
testcase_85 AC 96 ms
14,228 KB
testcase_86 AC 106 ms
12,836 KB
testcase_87 AC 280 ms
12,752 KB
testcase_88 AC 265 ms
12,848 KB
testcase_89 AC 2 ms
4,376 KB
testcase_90 AC 2 ms
4,376 KB
testcase_91 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
/home/judge/data/code/Main.nim(1, 28) Warning: use `std/random` instead; mersenne is deprecated [Deprecated]

ソースコード

diff #

import strutils, sequtils, mersenne

var mt = newMersenneTwister(0)

type Node = ref object
  val: int64
  size: int
  sum: int64
  left, right: Node

proc newNode(val: int64): Node =
  let node = new Node
  node.val = val
  node.size = 1
  node.sum = val
  return node

type Pair = Node

proc makePair(left, right: Node): Pair =
  let pair = new Pair
  pair.left = left
  pair.right = right
  return pair

proc size(root: Node): int =
  return if root == nil: 0 else: root.size
proc sum(root: Node): int64 =
  return if root == nil: 0.int64 else: root.sum

proc update(node: Node): Node =
  node.size = 1 + size(node.left) + size(node.right)
  node.sum = node.val + sum(node.left) + sum(node.right)
  return node

# max(left) <= min(right) じゃないとダメ?
proc merge(left, right: Node): Node =
  if left == nil:
    return right
  elif right == nil:
    return left
  if mt.getNum().int mod (size(left) + size(right)) < size(left):
    left.right = merge(left.right, right)
    return update(left)
  else:
    right.left = merge(left, right.left)
    return update(right)

# k 個と size(root)-k 個に分ける
proc split(root: Node, k: int): Pair =
  if root == nil:
    return makePair(root, root)
  if k <= size(root.left):
    let pair = split(root.left, k)
    root.left = pair.right
    return makePair(pair.left, update(root))
  else:
    let pair = split(root.right, k - size(root.left) - 1)
    root.right = pair.left
    return makePair(update(root), pair.right)

proc countLess(root: Node, x: int64): int =
  if root == nil:
    return 0
  if root.val < x:
    return size(root.left) + 1 + countLess(root.right, x)
  else:
    return countLess(root.left, x) # root.val==x の分はカウントしない

proc insert(root: Node, x: int64): Node =
  let
    k = root.countLess(x)
    pair = split(root, k)     # pair.left には x 未満しかない
  return merge(merge(pair.left, newNode(x)), pair.right)

proc erase(root: Node, x: int64): Node =
  let
    k = root.countLess(x)
    pair = split(root, k)
  if pair.right != nil:
    let pp = split(pair.right, 1) # pp.left は x のみ
    return merge(pair.left, pp.right)
  else: # x が存在しない
    return merge(pair.left, pair.right)

proc getKth(root: Node, k: int): Node =
  if root == nil or size(root) < k:
    return nil
  if size(root.left) + 1 == k:
    return root
  elif size(root.left) >= k:
    return getKth(root.left, k)
  else:
    return getKth(root.right, k - size(root.left) - 1)

proc main() =
  let
    nk = stdin.readLine.strip.split.map(parseInt)
    (n, k) = (nk[0], nk[1])
    a = stdin.readLine.strip.split.map(parseBiggestInt)

  var
    root: Node = nil
    ans = int64.high
  for i in 0..<n:
    root = root.insert(a[i])
    if i >= k:
      root = root.erase(a[i - k])
    if i + 1 >= k:
      let
        kth = root.getKth((k + 1) div 2)
        pair = root.split((k + 1) div 2)
      var cost: int64 = 0
      cost += kth.val * size(pair.left) - sum(pair.left)
      cost += sum(pair.right) - kth.val * size(pair.right)
      if cost < ans:
        ans = cost
      root = merge(pair.left, pair.right)
  echo ans

main()
0