結果

問題 No.59 鉄道の旅
ユーザー はむ吉🐹はむ吉🐹
提出日時 2016-09-15 17:52:45
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 527 ms / 5,000 ms
コード長 1,064 bytes
コンパイル時間 95 ms
コンパイル使用メモリ 11,040 KB
実行使用メモリ 28,012 KB
最終ジャッジ日時 2023-08-26 07:19:50
合計ジャッジ時間 2,825 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
8,656 KB
testcase_01 AC 15 ms
8,608 KB
testcase_02 AC 15 ms
8,728 KB
testcase_03 AC 15 ms
8,568 KB
testcase_04 AC 378 ms
21,736 KB
testcase_05 AC 21 ms
16,208 KB
testcase_06 AC 20 ms
16,172 KB
testcase_07 AC 20 ms
16,260 KB
testcase_08 AC 72 ms
17,308 KB
testcase_09 AC 60 ms
16,892 KB
testcase_10 AC 66 ms
17,216 KB
testcase_11 AC 37 ms
8,756 KB
testcase_12 AC 202 ms
9,592 KB
testcase_13 AC 385 ms
24,056 KB
testcase_14 AC 527 ms
28,012 KB
testcase_15 AC 21 ms
16,132 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/env python3

import collections


class FenwickTree(object):

    def __init__(self, num_elems):
        self.num_elems = num_elems
        self.data = [0] * 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))
    packs = collections.Counter()
    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)
            packs[w] += 1
        elif w < 0 and packs[abs(w)] > 0:
            ft.add(abs(w), -1)
            packs[abs(w)] -= 1
    print(ft.sum_to(max_weight + 1))


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