結果

問題 No.59 鉄道の旅
ユーザー brthyyjpbrthyyjp
提出日時 2021-11-01 02:53:50
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 167 ms / 5,000 ms
コード長 1,687 bytes
コンパイル時間 277 ms
コンパイル使用メモリ 81,980 KB
実行使用メモリ 108,524 KB
最終ジャッジ日時 2024-04-17 19:18:44
合計ジャッジ時間 2,053 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 36 ms
53,072 KB
testcase_01 AC 34 ms
53,256 KB
testcase_02 AC 35 ms
53,372 KB
testcase_03 AC 33 ms
52,396 KB
testcase_04 AC 167 ms
95,212 KB
testcase_05 AC 36 ms
53,552 KB
testcase_06 AC 34 ms
53,020 KB
testcase_07 AC 35 ms
53,440 KB
testcase_08 AC 65 ms
77,436 KB
testcase_09 AC 75 ms
76,912 KB
testcase_10 AC 72 ms
77,228 KB
testcase_11 AC 49 ms
70,688 KB
testcase_12 AC 71 ms
77,508 KB
testcase_13 AC 135 ms
108,524 KB
testcase_14 AC 131 ms
97,096 KB
testcase_15 AC 34 ms
53,572 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class BIT:
    def __init__(self, n):
        self.n = n
        self.bit = [0]*(self.n+1) # 1-indexed

    def init(self, init_val):
        for i, v in enumerate(init_val):
            self.add(i, v)

    def add(self, i, x):
        # i: 0-indexed
        i += 1 # to 1-indexed
        while i <= self.n:
            self.bit[i] += x
            i += (i & -i)

    def sum(self, i, j):
        # return sum of [i, j)
        # i, j: 0-indexed
        return self._sum(j) - self._sum(i)

    def _sum(self, i):
        # return sum of [0, i)
        # i: 0-indexed
        res = 0
        while i > 0:
            res += self.bit[i]
            i -= i & (-i)
        return res

    def lower_bound(self, x):
        s = 0
        pos = 0
        depth = self.n.bit_length()
        v = 1 << depth
        for i in range(depth, -1, -1):
            k = pos + v
            if k <= self.n and s + self.bit[k] < x:
                    s += self.bit[k]
                    pos += v
            v >>= 1
        return pos

    def __str__(self): # for debug
        arr = [self.sum(i,i+1) for i in range(self.n)]
        return str(arr)

import sys
import io, os
input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline

n, k = map(int, input().split())
W = [int(input()) for i in range(n)]
X = [abs(w) for w in W]
X = set(X)
X = list(X)
X.sort()
toid = {}
for i, x in enumerate(X):
    toid[x] = i
N = len(toid)
bit = BIT(N+1)
for w in W:
    if w > 0:
        w = toid[w]
        if bit.sum(w, bit.n) >= k:
            continue
        else:
            bit.add(w, 1)
    else:
        w = toid[-w]
        if bit.sum(w, w+1) >= 1:
            bit.add(w, -1)
print(bit.sum(0, bit.n))
0