結果

問題 No.180 美しいWhitespace (2)
ユーザー Mao-betaMao-beta
提出日時 2024-04-02 12:07:26
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 674 ms / 5,000 ms
コード長 2,301 bytes
コンパイル時間 412 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 237,212 KB
最終ジャッジ日時 2024-04-02 12:07:34
合計ジャッジ時間 6,795 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 48 ms
57,704 KB
testcase_01 AC 48 ms
57,704 KB
testcase_02 AC 47 ms
57,704 KB
testcase_03 AC 48 ms
57,704 KB
testcase_04 AC 81 ms
77,400 KB
testcase_05 AC 95 ms
85,432 KB
testcase_06 AC 125 ms
98,164 KB
testcase_07 AC 165 ms
117,844 KB
testcase_08 AC 202 ms
128,756 KB
testcase_09 AC 125 ms
76,704 KB
testcase_10 AC 125 ms
76,440 KB
testcase_11 AC 673 ms
237,212 KB
testcase_12 AC 674 ms
237,212 KB
testcase_13 AC 595 ms
237,212 KB
testcase_14 AC 319 ms
163,080 KB
testcase_15 AC 179 ms
98,268 KB
testcase_16 AC 148 ms
85,928 KB
testcase_17 AC 127 ms
78,024 KB
testcase_18 AC 120 ms
76,548 KB
testcase_19 AC 124 ms
76,568 KB
testcase_20 AC 47 ms
57,704 KB
testcase_21 AC 47 ms
57,704 KB
testcase_22 AC 48 ms
57,704 KB
testcase_23 AC 48 ms
57,704 KB
testcase_24 AC 47 ms
57,704 KB
testcase_25 AC 48 ms
57,704 KB
testcase_26 AC 48 ms
57,704 KB
testcase_27 AC 48 ms
57,704 KB
testcase_28 AC 48 ms
57,704 KB
testcase_29 AC 48 ms
57,704 KB
testcase_30 AC 104 ms
70,784 KB
testcase_31 AC 101 ms
70,784 KB
testcase_32 AC 71 ms
70,640 KB
testcase_33 AC 99 ms
76,540 KB
testcase_34 AC 100 ms
76,540 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
import math
import bisect
from heapq import heapify, heappop, heappush
from collections import deque, defaultdict, Counter
from functools import lru_cache
from itertools import accumulate, combinations, permutations, product

sys.setrecursionlimit(1000000)
MOD = 10 ** 9 + 7
MOD99 = 998244353

input = lambda: sys.stdin.readline().strip()
NI = lambda: int(input())
NMI = lambda: map(int, input().split())
NLI = lambda: list(NMI())
SI = lambda: input()
SMI = lambda: input().split()
SLI = lambda: list(SMI())
EI = lambda m: [NLI() for _ in range(m)]



from collections import deque

class ConvexHullTrick():
    """
    追加する直線の傾きが単調減少、計算する x 座標が単調増加するときに、
    直線(y = ai*x + bi)の追加とmin_i f(x)を計算 O(N+Q)
    """
    def __init__(self):
        self.deq = deque()

    def check(self, f1, f2, f3):
        return (f2[0] - f1[0]) * (f3[1] - f2[1]) >= (f2[1] - f1[1]) * (f3[0] - f2[0])

    def f(self, f1, x):
        return f1[0] * x + f1[1]

    # add f_i(x)=a*x+b
    def add_line(self, a, b):
        f1 = (a, b)
        while len(self.deq) >= 2 and self.check(self.deq[-2], self.deq[-1], f1):
            self.deq.pop()
        self.deq.append(f1)

    # min f_i(x)
    def query(self, x):
        while len(self.deq) >= 2 and self.f(self.deq[0], x) >= self.f(self.deq[1], x):
            self.deq.popleft()
        return self.f(self.deq[0], x)


def main():
    N = NI()
    AB = EI(N)
    X = set()
    for i in range(N):
        for j in range(i+1, N):
            ai, bi = AB[i]
            aj, bj = AB[j]
            if bi == bj:
                continue
            # ai-aj + (bi-bj)x = 0
            x = (aj-ai) // (bi-bj)
            X.add(x-1)
            X.add(x)
            X.add(x+1)
    X = sorted(list(X))
    CHTmax = ConvexHullTrick()
    CHTmin = ConvexHullTrick()
    AB.sort(key=lambda x: -x[1])
    for a, b in AB:
        CHTmin.add_line(b, a)
    AB.sort(key=lambda x: x[1])
    for a, b in AB:
        CHTmax.add_line(-b, -a)

    m = 10**20
    ans = 1
    for x in X:
        if x <= 0:
            continue
        res = -CHTmax.query(x) - CHTmin.query(x)
        if res < m:
            ans = x
            m = min(m, res)
    print(ans)


if __name__ == "__main__":
    main()
0