結果

問題 No.1251 絶対に間違ってはいけない最小化問題
ユーザー norioc
提出日時 2024-06-21 01:17:42
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 378 ms / 2,000 ms
コード長 545 bytes
コンパイル時間 385 ms
コンパイル使用メモリ 82,396 KB
実行使用メモリ 110,000 KB
最終ジャッジ日時 2024-06-21 01:18:06
合計ジャッジ時間 22,825 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 42
権限があれば一括ダウンロードができます

ソースコード

diff #

def ternary_search(f, lo, hi) -> float:
    l = lo
    r = hi
    for _ in range(100):
        c1 = (l * 2 + r) / 3
        c2 = (l + r * 2) / 3
        if f(c1) > f(c2):
            l = c1
        else:
            r = c2
    return l


N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))


def f(x):
    res = 0
    for a, b in zip(A, B):
        res += b * abs(x - a)

    return res


lo = min(A)
hi = max(A)
x = int(ternary_search(f, lo, hi))

v, x = min([(f(i), i) for i in [x-1, x, x+1]])
print(x, v)
0