結果

問題 No.198 キャンディー・ボックス2
ユーザー simansiman
提出日時 2016-03-27 02:17:49
言語 Ruby
(3.3.0)
結果
AC  
実行時間 88 ms / 1,000 ms
コード長 1,166 bytes
コンパイル時間 515 ms
コンパイル使用メモリ 11,424 KB
実行使用メモリ 15,344 KB
最終ジャッジ日時 2023-08-10 08:18:51
合計ジャッジ時間 3,845 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 88 ms
15,104 KB
testcase_01 AC 80 ms
15,164 KB
testcase_02 AC 80 ms
15,084 KB
testcase_03 AC 81 ms
15,156 KB
testcase_04 AC 80 ms
15,036 KB
testcase_05 AC 83 ms
15,156 KB
testcase_06 AC 79 ms
15,156 KB
testcase_07 AC 80 ms
15,044 KB
testcase_08 AC 80 ms
15,140 KB
testcase_09 AC 79 ms
15,308 KB
testcase_10 AC 79 ms
15,308 KB
testcase_11 AC 82 ms
15,324 KB
testcase_12 AC 82 ms
15,292 KB
testcase_13 AC 80 ms
15,176 KB
testcase_14 AC 80 ms
15,160 KB
testcase_15 AC 78 ms
15,164 KB
testcase_16 AC 82 ms
15,184 KB
testcase_17 AC 80 ms
15,152 KB
testcase_18 AC 80 ms
15,252 KB
testcase_19 AC 82 ms
15,036 KB
testcase_20 AC 81 ms
15,156 KB
testcase_21 AC 82 ms
15,048 KB
testcase_22 AC 83 ms
15,344 KB
testcase_23 AC 82 ms
15,088 KB
testcase_24 AC 83 ms
15,292 KB
testcase_25 AC 81 ms
15,184 KB
testcase_26 AC 82 ms
15,144 KB
testcase_27 AC 81 ms
15,124 KB
testcase_28 AC 80 ms
15,044 KB
testcase_29 AC 83 ms
15,116 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.rb:13: warning: assigned but unused variable - answer
Syntax OK

ソースコード

diff #

class Yukicoder
  attr_reader :candy, :remain

  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(:+)

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

      v1 = calc(c)
      v2 = calc(d)

      if v1 < v2 || v2 == Float::INFINITY
        b = d
      else
        a = c+1
      end
    end

    min_val = Float::INFINITY

    (a-100).upto(b+100) do |i|
      up_val = 0
      down_val = 0

      candy.each do |c|
        if c < i
          up_val += i-c
        else
          down_val += c-i
        end
      end

      if up_val <= remain + down_val
        min_val = [min_val, down_val + up_val].min
      end
    end

    puts min_val
  end

  def calc(i)
    up_val = 0
    down_val = 0

    candy.each do |c|
      if c < i
        up_val += i-c
      else
        down_val += c-i
      end
    end

    if up_val <= remain + down_val
      down_val + up_val
    else
      Float::INFINITY
    end
  end
end

Yukicoder.new
0