結果

問題 No.1251 絶対に間違ってはいけない最小化問題
ユーザー simansiman
提出日時 2022-08-05 13:28:56
言語 Ruby
(3.3.0)
結果
AC  
実行時間 1,726 ms / 2,000 ms
コード長 494 bytes
コンパイル時間 181 ms
コンパイル使用メモリ 11,320 KB
実行使用メモリ 43,908 KB
最終ジャッジ日時 2023-10-13 12:22:49
合計ジャッジ時間 48,876 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 82 ms
15,272 KB
testcase_01 AC 81 ms
15,348 KB
testcase_02 AC 80 ms
15,108 KB
testcase_03 AC 79 ms
15,312 KB
testcase_04 AC 80 ms
15,040 KB
testcase_05 AC 80 ms
15,136 KB
testcase_06 AC 79 ms
15,304 KB
testcase_07 AC 81 ms
15,096 KB
testcase_08 AC 79 ms
15,104 KB
testcase_09 AC 80 ms
15,052 KB
testcase_10 AC 80 ms
15,056 KB
testcase_11 AC 79 ms
15,176 KB
testcase_12 AC 80 ms
15,316 KB
testcase_13 AC 80 ms
15,300 KB
testcase_14 AC 78 ms
15,180 KB
testcase_15 AC 80 ms
15,048 KB
testcase_16 AC 80 ms
15,292 KB
testcase_17 AC 83 ms
15,176 KB
testcase_18 AC 1,629 ms
43,520 KB
testcase_19 AC 1,574 ms
43,516 KB
testcase_20 AC 945 ms
31,988 KB
testcase_21 AC 203 ms
16,360 KB
testcase_22 AC 1,014 ms
32,148 KB
testcase_23 AC 167 ms
15,820 KB
testcase_24 AC 1,315 ms
41,660 KB
testcase_25 AC 985 ms
32,020 KB
testcase_26 AC 395 ms
20,304 KB
testcase_27 AC 175 ms
16,268 KB
testcase_28 AC 1,697 ms
43,760 KB
testcase_29 AC 1,703 ms
43,696 KB
testcase_30 AC 1,693 ms
43,888 KB
testcase_31 AC 1,693 ms
43,708 KB
testcase_32 AC 1,693 ms
43,908 KB
testcase_33 AC 1,693 ms
43,844 KB
testcase_34 AC 1,713 ms
43,684 KB
testcase_35 AC 1,698 ms
43,744 KB
testcase_36 AC 1,726 ms
43,620 KB
testcase_37 AC 1,697 ms
43,604 KB
testcase_38 AC 1,702 ms
43,900 KB
testcase_39 AC 1,692 ms
43,896 KB
testcase_40 AC 1,693 ms
43,592 KB
testcase_41 AC 1,701 ms
43,648 KB
testcase_42 AC 1,702 ms
43,880 KB
testcase_43 AC 80 ms
15,292 KB
testcase_44 AC 80 ms
15,340 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

def ternary_search_tree(left, right)
  loop_cnt = 40

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

  (left + right) / 2
end

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

def f(x)
  C.sum { |a, b| b * (x - a).abs }
end

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

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