結果

問題 No.1251 絶対に間違ってはいけない最小化問題
ユーザー simansiman
提出日時 2022-08-05 13:18:24
言語 Ruby
(3.3.0)
結果
WA  
実行時間 -
コード長 533 bytes
コンパイル時間 348 ms
コンパイル使用メモリ 11,340 KB
実行使用メモリ 15,388 KB
最終ジャッジ日時 2023-10-13 12:08:50
合計ジャッジ時間 14,718 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 104 ms
15,336 KB
testcase_01 AC 94 ms
15,272 KB
testcase_02 AC 122 ms
15,156 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 TLE -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

def ternary_search_tree(left, right)
  loop_cnt = 10000

  loop_cnt.times do
    if yield((left * 2 + right) / 3.0) < yield((left + right * 2) / 3.0)
      right = (left + right * 2) / 3.0
    else
      left = (left * 2 + right) / 3.0
    end
  end

  (left + right) / 2.0
end

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

def f(x)
  sum = 0

  N.times do |i|
    sum += B[i] * (x - A[i]).abs
  end

  sum
end

res = ternary_search_tree(-1 * 10 ** 7, 10 ** 7) { |x| f(x) }

puts [res, f(res).to_i].join(' ')
0