結果

問題 No.198 キャンディー・ボックス2
ユーザー simansiman
提出日時 2016-03-27 02:01:01
言語 Ruby
(3.3.0)
結果
WA  
実行時間 -
コード長 843 bytes
コンパイル時間 38 ms
コンパイル使用メモリ 7,424 KB
実行使用メモリ 12,416 KB
最終ジャッジ日時 2024-04-10 00:16:11
合計ジャッジ時間 3,702 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 94 ms
12,288 KB
testcase_01 AC 87 ms
12,160 KB
testcase_02 AC 88 ms
12,160 KB
testcase_03 AC 89 ms
12,288 KB
testcase_04 AC 89 ms
12,160 KB
testcase_05 AC 86 ms
12,160 KB
testcase_06 AC 86 ms
12,160 KB
testcase_07 AC 88 ms
12,160 KB
testcase_08 AC 89 ms
12,160 KB
testcase_09 AC 87 ms
12,160 KB
testcase_10 AC 87 ms
12,160 KB
testcase_11 AC 87 ms
12,288 KB
testcase_12 AC 87 ms
12,160 KB
testcase_13 AC 89 ms
12,160 KB
testcase_14 AC 93 ms
12,416 KB
testcase_15 AC 92 ms
12,288 KB
testcase_16 AC 88 ms
12,160 KB
testcase_17 AC 87 ms
12,160 KB
testcase_18 AC 86 ms
12,160 KB
testcase_19 WA -
testcase_20 AC 88 ms
12,416 KB
testcase_21 WA -
testcase_22 AC 89 ms
12,160 KB
testcase_23 WA -
testcase_24 AC 85 ms
12,160 KB
testcase_25 WA -
testcase_26 AC 86 ms
12,160 KB
testcase_27 AC 85 ms
12,160 KB
testcase_28 WA -
testcase_29 AC 87 ms
12,416 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.rb:3: warning: assigned but unused variable - remain
Main.rb:11: warning: assigned but unused variable - answer
Syntax OK

ソースコード

diff #

class Yukicoder
  def initialize
    remain = gets.to_i
    n = gets.to_i
    candy = []

    n.times do
      candy << gets.to_i
    end

    answer = 0

    if candy.size == 1
      puts "0"
      return
    end

    a = 0
    b = candy.max
    answer = candy.inject(:+)

    #0.upto(b) do |j|
    #  puts "num = #{candy.inject(0){|sum,i| sum += (j < i)? i-j : j-i}}"
    #end

    while a < b-2
      c = a + (b-a)/3
      d = [a + 2 * ((b-a)/3), b].min

      v1 = candy.inject(0){|sum,i| sum += (c < i)? i-c : c-i}
      v2 = candy.inject(0){|sum,i| sum += (d < i)? i-d : d-i}

      if v1 < v2
        b = d
      else
        a = c+1
      end
    end

    min_val = Float::INFINITY

    a.upto(b) do |j|
      min_val = [min_val, candy.inject(0){|sum,i| sum += (j < i)? i-j : j-i}].min
    end
    puts min_val
  end
end

Yukicoder.new
0