結果

問題 No.59 鉄道の旅
ユーザー tobusakana
提出日時 2022-11-07 00:56:53
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 169 ms / 5,000 ms
コード長 815 bytes
コンパイル時間 171 ms
コンパイル使用メモリ 81,656 KB
実行使用メモリ 84,280 KB
最終ジャッジ日時 2024-07-20 11:27:02
合計ジャッジ時間 2,227 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 12
権限があれば一括ダウンロードができます

ソースコード

diff #

class FenwickTree: # (BinaryIndexedTree)
  def __init__(self, n):
    self.size = n
    self.tree = [0] * (n + 1)
 
  def sum(self, i):
    out = 0
    while i > 0:
      out += self.tree[i]
      i -= (i & -i)
    return out
 
  def add(self, i, x):
    while i <= self.size:
      self.tree[i] += x
      i += (i & -i)
      
N,K = map(int,input().split())
M = 1000001

BIT = FenwickTree(M + 1)
for _ in range(N):
    W = int(input())
    if W > 0: # 荷物を積む
        cnt = BIT.sum(M) - BIT.sum(W - 1) # その荷物以上の荷物の重さ
        if cnt < K: # 積める
            BIT.add(W, 1)
    else: # 荷物を下ろす
        W *= (-1)
        cnt = BIT.sum(W) - BIT.sum(W - 1)
        if cnt > 0:
            BIT.add(W, -1)
            
print(BIT.sum(M))
        
            
    
    
    

0