結果

問題 No.59 鉄道の旅
ユーザー flippergoflippergo
提出日時 2021-02-28 12:15:51
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 202 ms / 5,000 ms
コード長 818 bytes
コンパイル時間 424 ms
コンパイル使用メモリ 82,176 KB
実行使用メモリ 95,872 KB
最終ジャッジ日時 2024-10-02 18:12:47
合計ジャッジ時間 2,915 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 12
権限があれば一括ダウンロードができます

ソースコード

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