結果

問題 No.59 鉄道の旅
ユーザー はむ吉🐹はむ吉🐹
提出日時 2016-09-15 17:55:48
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 624 ms / 5,000 ms
コード長 1,078 bytes
コンパイル時間 322 ms
コンパイル使用メモリ 10,920 KB
実行使用メモリ 28,008 KB
最終ジャッジ日時 2023-08-26 07:20:07
合計ジャッジ時間 3,476 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 19 ms
8,592 KB
testcase_01 AC 19 ms
8,532 KB
testcase_02 AC 19 ms
8,540 KB
testcase_03 AC 20 ms
8,604 KB
testcase_04 AC 460 ms
21,604 KB
testcase_05 AC 55 ms
16,536 KB
testcase_06 AC 56 ms
16,484 KB
testcase_07 AC 56 ms
16,532 KB
testcase_08 AC 117 ms
17,280 KB
testcase_09 AC 96 ms
16,788 KB
testcase_10 AC 103 ms
17,072 KB
testcase_11 AC 41 ms
8,676 KB
testcase_12 AC 229 ms
9,532 KB
testcase_13 AC 462 ms
24,024 KB
testcase_14 AC 624 ms
28,008 KB
testcase_15 AC 55 ms
16,456 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/env python3

import collections


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))
    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