結果
問題 | No.738 平らな農地 |
ユーザー | ikd |
提出日時 | 2019-04-18 12:43:12 |
言語 | Nim (2.0.2) |
結果 |
AC
|
実行時間 | 680 ms / 2,000 ms |
コード長 | 3,133 bytes |
コンパイル時間 | 3,639 ms |
コンパイル使用メモリ | 66,048 KB |
実行使用メモリ | 18,176 KB |
最終ジャッジ日時 | 2024-07-01 23:14:18 |
合計ジャッジ時間 | 32,721 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
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 | 2 ms
5,376 KB |
testcase_04 | AC | 2 ms
5,376 KB |
testcase_05 | AC | 10 ms
5,376 KB |
testcase_06 | AC | 11 ms
5,376 KB |
testcase_07 | AC | 13 ms
5,376 KB |
testcase_08 | AC | 6 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 | 10 ms
5,376 KB |
testcase_14 | AC | 4 ms
5,376 KB |
testcase_15 | AC | 599 ms
12,288 KB |
testcase_16 | AC | 605 ms
12,288 KB |
testcase_17 | AC | 680 ms
10,112 KB |
testcase_18 | AC | 602 ms
9,984 KB |
testcase_19 | AC | 453 ms
9,344 KB |
testcase_20 | AC | 606 ms
12,288 KB |
testcase_21 | AC | 653 ms
9,728 KB |
testcase_22 | AC | 621 ms
11,264 KB |
testcase_23 | AC | 627 ms
9,472 KB |
testcase_24 | AC | 563 ms
9,216 KB |
testcase_25 | AC | 5 ms
5,376 KB |
testcase_26 | AC | 5 ms
5,376 KB |
testcase_27 | AC | 5 ms
5,376 KB |
testcase_28 | AC | 5 ms
5,376 KB |
testcase_29 | AC | 5 ms
5,376 KB |
testcase_30 | AC | 5 ms
5,376 KB |
testcase_31 | AC | 5 ms
5,376 KB |
testcase_32 | AC | 4 ms
5,376 KB |
testcase_33 | AC | 5 ms
5,376 KB |
testcase_34 | AC | 5 ms
5,376 KB |
testcase_35 | AC | 5 ms
5,376 KB |
testcase_36 | AC | 5 ms
5,376 KB |
testcase_37 | AC | 5 ms
5,376 KB |
testcase_38 | AC | 5 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 | 5 ms
5,376 KB |
testcase_43 | AC | 4 ms
5,376 KB |
testcase_44 | AC | 4 ms
5,376 KB |
testcase_45 | AC | 632 ms
14,592 KB |
testcase_46 | AC | 598 ms
12,288 KB |
testcase_47 | AC | 620 ms
13,824 KB |
testcase_48 | AC | 572 ms
14,464 KB |
testcase_49 | AC | 561 ms
13,568 KB |
testcase_50 | AC | 573 ms
13,952 KB |
testcase_51 | AC | 650 ms
13,824 KB |
testcase_52 | AC | 595 ms
13,184 KB |
testcase_53 | AC | 620 ms
13,440 KB |
testcase_54 | AC | 650 ms
13,696 KB |
testcase_55 | AC | 655 ms
12,800 KB |
testcase_56 | AC | 636 ms
13,568 KB |
testcase_57 | AC | 593 ms
12,928 KB |
testcase_58 | AC | 622 ms
14,336 KB |
testcase_59 | AC | 635 ms
14,720 KB |
testcase_60 | AC | 612 ms
14,720 KB |
testcase_61 | AC | 623 ms
13,952 KB |
testcase_62 | AC | 613 ms
13,312 KB |
testcase_63 | AC | 674 ms
13,312 KB |
testcase_64 | AC | 644 ms
13,952 KB |
testcase_65 | AC | 66 ms
9,216 KB |
testcase_66 | AC | 69 ms
9,344 KB |
testcase_67 | AC | 316 ms
15,104 KB |
testcase_68 | AC | 324 ms
15,616 KB |
testcase_69 | AC | 343 ms
11,904 KB |
testcase_70 | AC | 324 ms
15,104 KB |
testcase_71 | AC | 326 ms
8,704 KB |
testcase_72 | AC | 275 ms
11,904 KB |
testcase_73 | AC | 294 ms
13,184 KB |
testcase_74 | AC | 458 ms
11,520 KB |
testcase_75 | AC | 367 ms
11,648 KB |
testcase_76 | AC | 378 ms
14,848 KB |
testcase_77 | AC | 306 ms
10,496 KB |
testcase_78 | AC | 335 ms
11,136 KB |
testcase_79 | AC | 503 ms
11,904 KB |
testcase_80 | AC | 490 ms
14,080 KB |
testcase_81 | AC | 468 ms
12,032 KB |
testcase_82 | AC | 441 ms
11,008 KB |
testcase_83 | AC | 375 ms
11,264 KB |
testcase_84 | AC | 548 ms
11,008 KB |
testcase_85 | AC | 92 ms
9,856 KB |
testcase_86 | AC | 100 ms
9,728 KB |
testcase_87 | AC | 346 ms
18,176 KB |
testcase_88 | AC | 328 ms
17,536 KB |
testcase_89 | AC | 2 ms
5,376 KB |
testcase_90 | AC | 1 ms
5,376 KB |
testcase_91 | AC | 1 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 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()