結果

問題 No.180 美しいWhitespace (2)
ユーザー mkawa2mkawa2
提出日時 2020-01-27 10:55:02
言語 Python3
(3.11.6 + numpy 1.26.0 + scipy 1.11.3)
結果
AC  
実行時間 26 ms / 5,000 ms
コード長 1,774 bytes
コンパイル時間 254 ms
コンパイル使用メモリ 11,132 KB
実行使用メモリ 8,416 KB
最終ジャッジ日時 2023-10-12 12:52:35
合計ジャッジ時間 2,366 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 17 ms
8,380 KB
testcase_01 AC 16 ms
8,116 KB
testcase_02 AC 16 ms
8,328 KB
testcase_03 AC 16 ms
8,328 KB
testcase_04 AC 17 ms
8,260 KB
testcase_05 AC 17 ms
8,256 KB
testcase_06 AC 19 ms
8,416 KB
testcase_07 AC 19 ms
8,324 KB
testcase_08 AC 20 ms
8,216 KB
testcase_09 AC 25 ms
8,184 KB
testcase_10 AC 25 ms
8,008 KB
testcase_11 AC 26 ms
8,208 KB
testcase_12 AC 25 ms
8,120 KB
testcase_13 AC 25 ms
8,112 KB
testcase_14 AC 25 ms
8,008 KB
testcase_15 AC 26 ms
8,208 KB
testcase_16 AC 26 ms
8,008 KB
testcase_17 AC 25 ms
8,104 KB
testcase_18 AC 26 ms
8,060 KB
testcase_19 AC 25 ms
8,092 KB
testcase_20 AC 17 ms
8,276 KB
testcase_21 AC 16 ms
8,372 KB
testcase_22 AC 16 ms
8,156 KB
testcase_23 AC 16 ms
8,260 KB
testcase_24 AC 16 ms
8,320 KB
testcase_25 AC 16 ms
8,340 KB
testcase_26 AC 16 ms
8,260 KB
testcase_27 AC 16 ms
8,344 KB
testcase_28 AC 16 ms
8,348 KB
testcase_29 AC 16 ms
8,200 KB
testcase_30 AC 25 ms
8,412 KB
testcase_31 AC 26 ms
8,088 KB
testcase_32 AC 24 ms
8,008 KB
testcase_33 AC 26 ms
8,036 KB
testcase_34 AC 25 ms
8,228 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SI(): return sys.stdin.readline()[:-1]

def main():
    def isl(m):
        mx0 = mx1 = 0
        mn0 = mn1 = inf
        for a, b in zip(aa, bb):
            v = a + b * (m - 1)
            if v > mx0: mx0 = v
            if v < mn0: mn0 = v
            v = a + b * m
            if v > mx1: mx1 = v
            if v < mn1: mn1 = v
        return mx0 - mn0 > mx1 - mn1

    def f(x):
        mx = 0
        mn = inf
        for a, b in zip(aa, bb):
            v = a + b * x
            if v > mx: mx = v
            if v < mn: mn = v
        return mx - mn

    inf = 10 ** 19
    n = II()
    aa = []
    bb = []
    for _ in range(n):
        a, b = MI()
        aa.append(a)
        bb.append(b)
    # y=a+bxとして、f(x)=max(y)-min(y)は下に凸の関数になる
    # 下に凸の関数の最小値は三分探索が一般的だが、今回はxが整数という制約があるので二分探索でいける
    # あるx1と、その左隣のx0(つまりx0=x1-1)があったとき
    # f(x0)>f(x1)となればx1は最小値を含む区間の左端になる
    # そうでなければx1は右端
    l = 0
    r = 10**9+1
    while l + 1 < r:
        m = (l + r) // 2
        if isl(m): l = m
        else: r = m
    # 一応lrの前後を含めて確認
    mn = inf
    ans = r
    for x in range(max(1, l - 1), r + 2):
        v = f(x)
        if v < mn:
            ans = x
            mn = v
    print(ans)

main()
0