結果

問題 No.59 鉄道の旅
コンテスト
ユーザー はむ吉🐹
提出日時 2016-09-15 17:55:48
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 869 ms / 5,000 ms
コード長 1,078 bytes
コンパイル時間 109 ms
コンパイル使用メモリ 12,672 KB
実行使用メモリ 30,412 KB
最終ジャッジ日時 2024-12-24 23:08:48
合計ジャッジ時間 4,327 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 12
権限があれば一括ダウンロードができます

ソースコード

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