結果

問題 No.929 よくあるボールを移動するやつ
ユーザー siman
提出日時 2020-09-16 09:33:35
言語 Ruby
(3.4.1)
結果
AC  
実行時間 140 ms / 2,000 ms
コード長 488 bytes
コンパイル時間 45 ms
コンパイル使用メモリ 7,552 KB
実行使用メモリ 21,376 KB
最終ジャッジ日時 2024-06-22 03:04:35
合計ジャッジ時間 2,374 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 12
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

N = gets.to_i
B = gets.split.map(&:to_i)

stock = []
empty = []
ans = 0

B.each_with_index do |b, idx|
  if b == 0
    if stock.empty?
      empty << idx
    else
      i, cnt = stock.pop

      ans += idx - i
      cnt -= 1

      stock.push([i, cnt]) if cnt > 0
    end
  else
    while empty.size > 0 && b > 0
      i = empty.pop
      ans += idx - i
      b -= 1
    end

    if b == 0
      empty.push(idx)
    elsif b >= 2
      stock.push([idx, b - 1])
    end
  end
end

puts ans
0