結果

問題 No.609 Noelちゃんと星々
ユーザー kichirb3
提出日時 2018-03-24 01:44:52
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 1,055 ms / 2,000 ms
コード長 1,045 bytes
コンパイル時間 87 ms
コンパイル使用メモリ 12,672 KB
実行使用メモリ 21,928 KB
最終ジャッジ日時 2024-06-25 02:37:26
合計ジャッジ時間 18,232 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 25
権限があれば一括ダウンロードができます

ソースコード

diff #

# -*- coding: utf-8 -*-
"""
No.609 Noelちゃんと星々
https://yukicoder.me/problems/no/609

"""
import sys
from sys import stdin
input = stdin.readline


def calc_score(n, stars):
    res = [abs(x - n) for x in stars]
    return sum(res)


def solve(stars):
    ub = max(stars)
    lb = min(stars)

    while True:
        diff = ub - lb
        if diff < 10:
            break
        mid1 = (lb*2 + ub) // 3
        mid2 = (lb + ub*2) // 3
        mid1_score = calc_score(mid1, stars)
        mid2_score = calc_score(mid2, stars)
        if mid2_score > mid1_score:
            ub = mid2
        else:
            lb = mid1

    ans = float('inf')
    for i in range(lb, ub+1):
        t = calc_score(i, stars)
        ans = min(ans, t)
    return ans



def main(args):
    #with open('609.input') as f:
    #     for line in f:
    #        stars = [int(x) for x in line.split()]
    N = int(input())
    stars = [int(x) for x in input().split()]
    ans = solve(stars)
    print(ans)


if __name__ == '__main__':
    main(sys.argv[1:])
0