結果

問題 No.59 鉄道の旅
ユーザー flippergoflippergo
提出日時 2021-02-28 12:15:51
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 177 ms / 5,000 ms
コード長 818 bytes
コンパイル時間 377 ms
コンパイル使用メモリ 82,412 KB
実行使用メモリ 95,592 KB
最終ジャッジ日時 2024-04-10 15:58:11
合計ジャッジ時間 2,491 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 37 ms
52,864 KB
testcase_01 AC 36 ms
52,336 KB
testcase_02 AC 36 ms
53,496 KB
testcase_03 AC 35 ms
53,000 KB
testcase_04 AC 177 ms
94,620 KB
testcase_05 AC 35 ms
53,300 KB
testcase_06 AC 36 ms
53,748 KB
testcase_07 AC 36 ms
52,832 KB
testcase_08 AC 79 ms
77,068 KB
testcase_09 AC 87 ms
77,228 KB
testcase_10 AC 86 ms
77,212 KB
testcase_11 AC 64 ms
73,972 KB
testcase_12 AC 98 ms
78,852 KB
testcase_13 AC 150 ms
93,936 KB
testcase_14 AC 142 ms
95,592 KB
testcase_15 AC 35 ms
53,168 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

n,K = map(int,input().split())
W = [int(input()) for _ in range(n)]
A = [abs(W[i]) for i in range(n)]
A = sorted(A)
D = {}
cnt = 1
for w in A:
    if w not in D:
        D[w] = cnt
        cnt += 1
k = 0
while 2**k<cnt:
    k += 1
N = 2**k
B = [0 for _ in range(N+1)]
def update(i,x):
    j = i
    while j<=N:
        B[j] += x
        j += j&(-j)
def csum(i):
    tot = 0
    j = i
    while j>0:
        tot += B[j]
        j -= j&(-j)
    return tot
for i in range(n):
    w = W[i]
    if w>0:
        j = D[w]
        if j==1:
            k = 0
        else:
            k = csum(j-1)
        if B[N]-k<K:
            update(j,1)
    else:
        w = -w
        j = D[w]
        if j==1:
            k = csum(1)
        else:
            k = csum(j)-csum(j-1)
        if k>0:
            update(j,-1)
print(B[N])
0