結果
問題 | No.738 平らな農地 |
ユーザー | ikd |
提出日時 | 2019-04-18 12:21:48 |
言語 | Nim (2.0.2) |
結果 |
AC
|
実行時間 | 511 ms / 2,000 ms |
コード長 | 3,016 bytes |
コンパイル時間 | 3,792 ms |
コンパイル使用メモリ | 65,988 KB |
実行使用メモリ | 18,176 KB |
最終ジャッジ日時 | 2024-07-01 23:11:25 |
合計ジャッジ時間 | 25,371 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 2 ms
5,376 KB |
testcase_02 | AC | 1 ms
5,376 KB |
testcase_03 | AC | 1 ms
5,376 KB |
testcase_04 | AC | 2 ms
5,376 KB |
testcase_05 | AC | 8 ms
5,376 KB |
testcase_06 | AC | 10 ms
5,376 KB |
testcase_07 | AC | 11 ms
5,376 KB |
testcase_08 | AC | 5 ms
5,376 KB |
testcase_09 | AC | 3 ms
5,376 KB |
testcase_10 | AC | 2 ms
5,376 KB |
testcase_11 | AC | 6 ms
5,376 KB |
testcase_12 | AC | 4 ms
5,376 KB |
testcase_13 | AC | 8 ms
5,376 KB |
testcase_14 | AC | 4 ms
5,376 KB |
testcase_15 | AC | 440 ms
12,160 KB |
testcase_16 | AC | 426 ms
12,288 KB |
testcase_17 | AC | 511 ms
10,112 KB |
testcase_18 | AC | 439 ms
9,984 KB |
testcase_19 | AC | 339 ms
9,344 KB |
testcase_20 | AC | 447 ms
12,288 KB |
testcase_21 | AC | 495 ms
9,728 KB |
testcase_22 | AC | 455 ms
11,264 KB |
testcase_23 | AC | 442 ms
9,472 KB |
testcase_24 | AC | 423 ms
9,216 KB |
testcase_25 | AC | 4 ms
5,376 KB |
testcase_26 | AC | 4 ms
5,376 KB |
testcase_27 | AC | 4 ms
5,376 KB |
testcase_28 | AC | 4 ms
5,376 KB |
testcase_29 | AC | 4 ms
5,376 KB |
testcase_30 | AC | 4 ms
5,376 KB |
testcase_31 | AC | 4 ms
5,376 KB |
testcase_32 | AC | 4 ms
5,376 KB |
testcase_33 | AC | 4 ms
5,376 KB |
testcase_34 | AC | 4 ms
5,376 KB |
testcase_35 | AC | 4 ms
5,376 KB |
testcase_36 | AC | 4 ms
5,376 KB |
testcase_37 | AC | 4 ms
5,376 KB |
testcase_38 | AC | 4 ms
5,376 KB |
testcase_39 | AC | 5 ms
5,376 KB |
testcase_40 | AC | 4 ms
5,376 KB |
testcase_41 | AC | 4 ms
5,376 KB |
testcase_42 | AC | 4 ms
5,376 KB |
testcase_43 | AC | 4 ms
5,376 KB |
testcase_44 | AC | 4 ms
5,376 KB |
testcase_45 | AC | 466 ms
14,720 KB |
testcase_46 | AC | 443 ms
12,288 KB |
testcase_47 | AC | 464 ms
13,824 KB |
testcase_48 | AC | 419 ms
14,464 KB |
testcase_49 | AC | 409 ms
13,440 KB |
testcase_50 | AC | 421 ms
13,952 KB |
testcase_51 | AC | 483 ms
13,696 KB |
testcase_52 | AC | 422 ms
13,184 KB |
testcase_53 | AC | 445 ms
13,184 KB |
testcase_54 | AC | 469 ms
13,696 KB |
testcase_55 | AC | 476 ms
12,672 KB |
testcase_56 | AC | 461 ms
13,696 KB |
testcase_57 | AC | 422 ms
12,928 KB |
testcase_58 | AC | 438 ms
14,336 KB |
testcase_59 | AC | 452 ms
14,720 KB |
testcase_60 | AC | 438 ms
14,720 KB |
testcase_61 | AC | 421 ms
14,080 KB |
testcase_62 | AC | 446 ms
13,184 KB |
testcase_63 | AC | 504 ms
13,312 KB |
testcase_64 | AC | 467 ms
13,824 KB |
testcase_65 | AC | 63 ms
9,216 KB |
testcase_66 | AC | 66 ms
9,344 KB |
testcase_67 | AC | 243 ms
15,104 KB |
testcase_68 | AC | 246 ms
15,616 KB |
testcase_69 | AC | 259 ms
11,776 KB |
testcase_70 | AC | 249 ms
15,104 KB |
testcase_71 | AC | 236 ms
8,704 KB |
testcase_72 | AC | 214 ms
11,904 KB |
testcase_73 | AC | 236 ms
13,184 KB |
testcase_74 | AC | 275 ms
11,520 KB |
testcase_75 | AC | 269 ms
11,648 KB |
testcase_76 | AC | 282 ms
14,848 KB |
testcase_77 | AC | 240 ms
10,496 KB |
testcase_78 | AC | 261 ms
11,264 KB |
testcase_79 | AC | 359 ms
11,904 KB |
testcase_80 | AC | 334 ms
14,080 KB |
testcase_81 | AC | 339 ms
12,160 KB |
testcase_82 | AC | 319 ms
11,008 KB |
testcase_83 | AC | 257 ms
11,264 KB |
testcase_84 | AC | 312 ms
11,008 KB |
testcase_85 | AC | 86 ms
9,856 KB |
testcase_86 | AC | 89 ms
9,728 KB |
testcase_87 | AC | 263 ms
18,176 KB |
testcase_88 | AC | 248 ms
16,896 KB |
testcase_89 | AC | 1 ms
5,376 KB |
testcase_90 | AC | 1 ms
5,376 KB |
testcase_91 | AC | 2 ms
5,376 KB |
コンパイルメッセージ
/home/judge/data/code/Main.nim(1, 28) Warning: use `std/random` instead; mersenne is deprecated [Deprecated]
ソースコード
import strutils, sequtils, mersenne var mt = newMersenneTwister(0) type Node = ref object val: int64 priority: int size: int sum: int64 left, right: Node proc newNode(val: int64): Node = let node = new Node node.val = val node.priority = mt.getNum().int 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 proc merge(left, right: Node): Node = if left == nil: return right elif right == nil: return left if left.priority > right.priority: left.right = merge(left.right, right) return update(left) else: right.left = merge(left, right.left) return update(right) 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) proc insert(root: Node, x: int64): Node = let k = root.countLess(x) pair = split(root, k) 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: return nil if 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]) # echo repr(root) 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()