結果

問題 No.77 レンガのピラミッド
ユーザー code-devocode-devo
提出日時 2016-01-21 09:17:45
言語 Ruby
(3.3.0)
結果
AC  
実行時間 159 ms / 5,000 ms
コード長 1,015 bytes
コンパイル時間 43 ms
コンパイル使用メモリ 11,900 KB
実行使用メモリ 16,132 KB
最終ジャッジ日時 2023-10-21 13:41:05
合計ジャッジ時間 3,690 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 92 ms
16,016 KB
testcase_01 AC 86 ms
16,016 KB
testcase_02 AC 87 ms
16,016 KB
testcase_03 AC 86 ms
16,016 KB
testcase_04 AC 86 ms
16,012 KB
testcase_05 AC 86 ms
16,016 KB
testcase_06 AC 159 ms
16,132 KB
testcase_07 AC 86 ms
16,016 KB
testcase_08 AC 136 ms
16,132 KB
testcase_09 AC 88 ms
16,016 KB
testcase_10 AC 88 ms
16,016 KB
testcase_11 AC 88 ms
16,016 KB
testcase_12 AC 106 ms
16,016 KB
testcase_13 AC 109 ms
16,016 KB
testcase_14 AC 131 ms
16,132 KB
testcase_15 AC 86 ms
16,012 KB
testcase_16 AC 87 ms
16,016 KB
testcase_17 AC 85 ms
16,016 KB
testcase_18 AC 106 ms
16,016 KB
testcase_19 AC 87 ms
16,016 KB
testcase_20 AC 88 ms
16,016 KB
testcase_21 AC 96 ms
16,016 KB
testcase_22 AC 89 ms
16,016 KB
testcase_23 AC 96 ms
16,016 KB
testcase_24 AC 87 ms
16,016 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

# うーむ・・・

N = gets.to_i
A = gets.split.map(&:to_i)
SUM = A.inject(&:+)

min_move = nil
for r in 1..100 do #ピラミッドの段数
  py = r ** 2 #ピラミッドの石数
  break if py > SUM #石が足りないなら終了

  for m in (r-1)..(N-1) do #ピラミッドの中央の列
    pl = 0
    mi = 0
    (m-r+1).upto(m+r-1).with_index do |c, idx|
      s = r - (r - idx - 1).abs #c列に必要な石数
      d = (c < N ? A[c] : 0) - s
      pl += d if d > 0
      mi += -d if d < 0
    end
    sum = SUM #存在する石数
    move1 = [pl, mi].min #ピラミッド構築範囲内で移動する。
    move2 = (pl - mi).abs #ピラミッド構築範囲内で余った石は捨てる。足りないなら構築範囲外から持ってくる。
    sum -= move2 if pl - mi >= 0 #捨てた場合、石が減る。
    move3 = sum - py #ピラミッド構築範囲外の石を捨てる。
    move = move1 + move2 + move3
    min_move = move if !min_move || move < min_move
  end
end

puts min_move
0