結果

問題 No.837 Noelちゃんと星々2
ユーザー letrangerjpletrangerjp
提出日時 2019-06-14 22:24:40
言語 Ruby
(3.3.0)
結果
WA  
実行時間 -
コード長 584 bytes
コンパイル時間 119 ms
コンパイル使用メモリ 7,424 KB
実行使用メモリ 75,264 KB
最終ジャッジ日時 2024-04-26 17:14:52
合計ジャッジ時間 11,988 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
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 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 AC 82 ms
12,032 KB
testcase_25 AC 81 ms
12,032 KB
testcase_26 AC 79 ms
12,160 KB
testcase_27 WA -
testcase_28 AC 83 ms
12,160 KB
testcase_29 AC 81 ms
12,032 KB
testcase_30 AC 80 ms
12,032 KB
testcase_31 AC 80 ms
12,032 KB
testcase_32 AC 82 ms
12,032 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

N = gets.to_i
Y = gets.split.map &:to_i
case Y.uniq.size
when 1
  p 1
when 2
  p 0
else
  Y.sort!
  cusum = [sum = 0] + Y.map{|y| sum += y }

  f = ->idx{
    lsum = cusum[idx]
    lave = lsum.fdiv(idx).round
    ldist = Y[0...idx].map{|n| (n - lave).abs }.sum

    rsum = sum - lsum
    rave = rsum.fdiv(N - idx).round
    rdist = Y[idx..-1].map{|n| (n - rave).abs }.sum
    
    ldist + rdist
  }

  l = 1
  r = N-1
  40.times{
    if f[(l * 2 + r) / 3] < f[(l + r * 2) / 3]
      r = (l + r * 2) / 3
    else
      l = (l * 2 + r) / 3
    end
  }
  # p [l,r]
  p f[(l + r) / 2]
end
0