結果

問題 No.609 Noelちゃんと星々
ユーザー daku9640
提出日時 2018-08-12 01:09:08
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 107 ms / 2,000 ms
コード長 556 bytes
コンパイル時間 83 ms
コンパイル使用メモリ 12,672 KB
実行使用メモリ 21,912 KB
最終ジャッジ日時 2024-09-23 06:39:40
合計ジャッジ時間 3,063 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 25
権限があれば一括ダウンロードができます

ソースコード

diff #

from bisect import bisect_left, bisect, insort_left, insort
from itertools import accumulate
n = int(input())
y = sorted(map(int, input().split()))
sum_y = tuple(accumulate(y))
m = {}
def f(a):
    if a not in m:
        l = bisect_left(y, a)
        m[a] = abs(sum_y[l - 1] - a * l) + abs((sum_y[-1] - sum_y[l - 1]) - a * (n - l))
    return m[a]
l = min(y)
r = max(y)
while r - l > 2:
    mid_l = (l * 2 + r) // 3
    mid_r = (l + r * 2) // 3
    if f(mid_l) > f(mid_r):
        l = mid_l
    else:
        r = mid_r
print(min(f(l), f(l + 1), f(l + 2)))
0