結果

問題 No.837 Noelちゃんと星々2
ユーザー TANIGUCHI KousukeTANIGUCHI Kousuke
提出日時 2019-12-12 13:02:52
言語 Ruby
(3.3.0)
結果
AC  
実行時間 203 ms / 2,000 ms
コード長 473 bytes
コンパイル時間 438 ms
コンパイル使用メモリ 11,308 KB
実行使用メモリ 23,756 KB
最終ジャッジ日時 2023-09-02 07:29:21
合計ジャッジ時間 6,235 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 201 ms
23,756 KB
testcase_01 AC 203 ms
23,616 KB
testcase_02 AC 201 ms
23,644 KB
testcase_03 AC 201 ms
23,672 KB
testcase_04 AC 200 ms
23,572 KB
testcase_05 AC 198 ms
23,584 KB
testcase_06 AC 199 ms
23,748 KB
testcase_07 AC 199 ms
23,620 KB
testcase_08 AC 195 ms
23,724 KB
testcase_09 AC 82 ms
15,192 KB
testcase_10 AC 84 ms
15,212 KB
testcase_11 AC 84 ms
15,072 KB
testcase_12 AC 84 ms
14,996 KB
testcase_13 AC 85 ms
15,068 KB
testcase_14 AC 87 ms
15,104 KB
testcase_15 AC 83 ms
15,220 KB
testcase_16 AC 86 ms
14,988 KB
testcase_17 AC 85 ms
15,212 KB
testcase_18 AC 85 ms
15,156 KB
testcase_19 AC 83 ms
15,112 KB
testcase_20 AC 83 ms
15,220 KB
testcase_21 AC 87 ms
15,200 KB
testcase_22 AC 84 ms
15,184 KB
testcase_23 AC 83 ms
15,308 KB
testcase_24 AC 83 ms
15,212 KB
testcase_25 AC 83 ms
15,272 KB
testcase_26 AC 85 ms
15,056 KB
testcase_27 AC 84 ms
15,108 KB
testcase_28 AC 84 ms
15,260 KB
testcase_29 AC 84 ms
15,104 KB
testcase_30 AC 89 ms
15,060 KB
testcase_31 AC 88 ms
15,256 KB
testcase_32 AC 85 ms
15,316 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

def min(a,b); a < b ? a : b; end
  
N = gets.to_i
Y = gets.split.map(&:to_i).sort
S = Array.new(N + 1, 0)


Y.each_with_index do |y,i|
  S[i + 1] = S[i] + y
end


ans = N.times.inject(Float::INFINITY) do |a, m|
  l = m / 2
  r = m + (N - m) / 2

  y1 = Y[l]
  y2 = Y[r]
  
  s1 = l * y1 - (S[l] - S[0])
  s2 = S[m] - S[l] - (m - l) * y1
  s3 = (r - m) * y2 - (S[r] - S[m])
  s4 = (S[N] - S[r]) - (N - r) * y2
  
  min(a, s1 + s2 + s3 + s4)
end

puts Y[0] == Y[-1] ? 1 : ans
0