結果

問題 No.96 圏外です。
ユーザー mkawa2mkawa2
提出日時 2020-01-17 01:12:56
言語 PyPy3
(7.3.15)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 3,643 bytes
コンパイル時間 594 ms
コンパイル使用メモリ 87,404 KB
実行使用メモリ 102,268 KB
最終ジャッジ日時 2023-09-07 01:01:34
合計ジャッジ時間 17,314 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 92 ms
72,316 KB
testcase_01 AC 92 ms
71,644 KB
testcase_02 AC 93 ms
72,208 KB
testcase_03 AC 94 ms
71,624 KB
testcase_04 AC 127 ms
78,676 KB
testcase_05 AC 135 ms
79,588 KB
testcase_06 AC 160 ms
81,020 KB
testcase_07 AC 215 ms
84,100 KB
testcase_08 AC 214 ms
84,696 KB
testcase_09 AC 256 ms
87,140 KB
testcase_10 AC 432 ms
91,892 KB
testcase_11 AC 400 ms
97,684 KB
testcase_12 AC 420 ms
101,948 KB
testcase_13 AC 658 ms
102,268 KB
testcase_14 TLE -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict
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)]

class UnionFind:
    def __init__(self, n):
        self.state = [-1] * n

    def root(self, u):
        pu = self.state[u]
        if pu < 0: return u
        res = self.state[u] = self.root(pu)
        return res

    def merge(self, u, v):
        ru = self.root(u)
        rv = self.root(v)
        state = self.state
        if ru == rv: return
        if state[ru] > state[rv]: ru, rv = rv, ru
        if state[ru] == state[rv]: state[ru] -= 1
        state[rv] = ru

def ConvexHull(xy):
    def NG(x, y):
        x0, y0 = res[-2]
        x1, y1 = res[-1]
        return (x - x0) * (y1 - y0) - (x1 - x0) * (y - y0) >= 0

    res = []
    xy.sort(key=lambda p: (p[0], p[1]))
    for x, y in xy:
        while len(res) > 1 and NG(x, y): res.pop()
        res.append((x, y))
    under_n = len(res)
    for x, y in xy[-2::-1]:
        while len(res) > under_n and NG(x, y): res.pop()
        res.append((x, y))
    return res[:-1]

def RotatingCalipers(xy):
    def dist(i, j):
        ix, iy = xy[i]
        jx, jy = xy[j]
        return (ix - jx) ** 2 + (iy - jy) ** 2

    def vec(i):
        x0, y0 = xy[i]
        x1, y1 = xy[(i + 1) % n]
        return x1 - x0, y1 - y0

    def outer(i, j):
        vix, viy = vec(i)
        vjx, vjy = vec(j)
        return vix * vjy - viy * vjx

    n = len(xy)
    if n < 2: return 0
    if n == 2: return dist(0, 1) ** 0.5
    res = 0
    i = xy.index(min(xy))
    j = xy.index(max(xy))
    si, sj = i, j
    while i != sj or j != si:
        res = max(res, dist(i, j))
        if outer(i, j) > 0:
            j = (j + 1) % n
        else:
            i = (i + 1) % n
    return res ** 0.5

def main():
    n = II()

    # 例外処理
    if n == 0:
        print(1)
        exit()

    # 負の座標が面倒なので10000を足して正にする
    # 座標平面を一辺10の正方形ゾーンに分割して、座標をゾーンごとに振り分ける
    zone = {}
    xy = []
    for i in range(n):
        x, y = MI()
        x, y = x + 10000, y + 10000
        xy.append((x, y))
        x, y = x // 10, y // 10
        zone.setdefault((x, y), [])
        zone[x, y].append(i)
    # print(zone)

    # 自分自身も含めて隣接9ゾーンで10km以内の座標同士を結合していく
    # 実際は未訪の5ゾーン(自分と上と右3つ)を調べればよい
    uf = UnionFind(n)
    for (zx, zy), ii in sorted(zone.items(), key=lambda k: (k[0], k[1])):
        for i in ii:
            ix, iy = xy[i]
            for nzx, nzy in [(zx, zy), (zx, zy + 1), (zx + 1, zy - 1), (zx + 1, zy), (zx + 1, zy + 1)]:
                if (nzx, nzy) not in zone: continue
                for j in zone[nzx, nzy]:
                    if i == j: continue
                    jx, jy = xy[j]
                    if (ix - jx) ** 2 + (iy - jy) ** 2 <= 100: uf.merge(i, j)

    # 結合成分どうしでグループ分け
    rtoxy = defaultdict(list)
    for i in range(n):
        rtoxy[uf.root(i)].append(xy[i])
    # print(rtoxy)

    # グループごとに最遠点を計算
    ans = 2
    for v in rtoxy.values():
        if len(v) == 1: continue
        # 凸包を作る
        cnv = ConvexHull(v)
        # 最大距離を求める
        ans = max(ans, RotatingCalipers(cnv) + 2)
    print(ans)

main()
0