結果

問題 No.59 鉄道の旅
ユーザー tobusakanatobusakana
提出日時 2022-11-07 00:56:53
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 174 ms / 5,000 ms
コード長 815 bytes
コンパイル時間 304 ms
コンパイル使用メモリ 86,764 KB
実行使用メモリ 85,556 KB
最終ジャッジ日時 2023-09-27 17:07:38
合計ジャッジ時間 3,133 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 69 ms
79,172 KB
testcase_01 AC 70 ms
79,060 KB
testcase_02 AC 70 ms
79,136 KB
testcase_03 AC 72 ms
79,136 KB
testcase_04 AC 174 ms
85,556 KB
testcase_05 AC 72 ms
83,352 KB
testcase_06 AC 71 ms
83,556 KB
testcase_07 AC 73 ms
83,180 KB
testcase_08 AC 107 ms
85,108 KB
testcase_09 AC 113 ms
85,488 KB
testcase_10 AC 113 ms
85,424 KB
testcase_11 AC 98 ms
85,236 KB
testcase_12 AC 133 ms
85,224 KB
testcase_13 AC 155 ms
85,260 KB
testcase_14 AC 151 ms
85,264 KB
testcase_15 AC 70 ms
79,012 KB
権限があれば一括ダウンロードができます

ソースコード

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