結果

問題 No.929 よくあるボールを移動するやつ
ユーザー simansiman
提出日時 2020-09-16 09:33:35
言語 Ruby
(3.3.0)
結果
AC  
実行時間 153 ms / 2,000 ms
コード長 488 bytes
コンパイル時間 284 ms
コンパイル使用メモリ 11,268 KB
実行使用メモリ 23,516 KB
最終ジャッジ日時 2023-09-04 03:22:45
合計ジャッジ時間 2,997 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 82 ms
15,088 KB
testcase_01 AC 85 ms
15,236 KB
testcase_02 AC 80 ms
15,188 KB
testcase_03 AC 81 ms
15,184 KB
testcase_04 AC 82 ms
15,096 KB
testcase_05 AC 83 ms
15,068 KB
testcase_06 AC 82 ms
15,288 KB
testcase_07 AC 107 ms
17,688 KB
testcase_08 AC 141 ms
22,176 KB
testcase_09 AC 144 ms
22,032 KB
testcase_10 AC 145 ms
23,088 KB
testcase_11 AC 141 ms
21,572 KB
testcase_12 AC 153 ms
23,392 KB
testcase_13 AC 152 ms
23,504 KB
testcase_14 AC 142 ms
22,428 KB
testcase_15 AC 147 ms
23,516 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
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