結果

問題 No.59 鉄道の旅
ユーザー はむ吉🐹はむ吉🐹
提出日時 2016-09-15 17:17:16
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 575 ms / 5,000 ms
コード長 986 bytes
コンパイル時間 319 ms
コンパイル使用メモリ 10,976 KB
実行使用メモリ 19,836 KB
最終ジャッジ日時 2023-08-26 07:19:57
合計ジャッジ時間 3,654 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
7,836 KB
testcase_01 AC 16 ms
7,888 KB
testcase_02 AC 16 ms
7,816 KB
testcase_03 AC 16 ms
7,776 KB
testcase_04 AC 547 ms
19,408 KB
testcase_05 AC 52 ms
16,100 KB
testcase_06 AC 54 ms
16,096 KB
testcase_07 AC 53 ms
16,136 KB
testcase_08 AC 111 ms
16,436 KB
testcase_09 AC 105 ms
16,516 KB
testcase_10 AC 104 ms
16,456 KB
testcase_11 AC 37 ms
8,336 KB
testcase_12 AC 228 ms
9,040 KB
testcase_13 AC 573 ms
19,708 KB
testcase_14 AC 575 ms
19,836 KB
testcase_15 AC 53 ms
16,284 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/env python3


class FenwickTree(object):

    def __init__(self, num_elems):
        self.num_elems = num_elems
        self.data = [0 for _ in range(num_elems)]

    def sum_to(self, end):
        s = 0
        i = end - 1
        while i >= 0:
            s += self.data[i]
            i = (i & (i + 1)) - 1
        return s

    def sum_range(self, start, end):
        return self.sum_to(end) - self.sum_to(start)

    def add(self, idx, x):
        while idx < self.num_elems:
            self.data[idx] += x
            idx |= idx + 1


def main():
    n, k = map(int, input().split())
    ws = [int(input()) for _ in range(n)]
    max_weight = max(map(abs, ws))
    ft = FenwickTree(max_weight + 1)
    for w in ws:
        if w > 0 and ft.sum_range(w, max_weight + 1) < k:
            ft.add(w, 1)
        elif w < 0 and ft.sum_range(abs(w), abs(w) + 1) > 0:
            ft.add(abs(w), -1)
    print(ft.sum_to(max_weight + 1))


if __name__ == '__main__':
    main()
0