結果

問題 No.96 圏外です。
ユーザー mkawa2mkawa2
提出日時 2020-01-17 01:10:40
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
WA  
実行時間 -
コード長 3,643 bytes
コンパイル時間 427 ms
コンパイル使用メモリ 11,296 KB
実行使用メモリ 82,140 KB
最終ジャッジ日時 2023-09-07 00:57:36
合計ジャッジ時間 19,402 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 20 ms
9,392 KB
testcase_01 WA -
testcase_02 AC 20 ms
9,156 KB
testcase_03 AC 19 ms
9,052 KB
testcase_04 AC 33 ms
10,704 KB
testcase_05 AC 42 ms
11,744 KB
testcase_06 AC 58 ms
13,296 KB
testcase_07 AC 87 ms
14,912 KB
testcase_08 AC 115 ms
18,484 KB
testcase_09 AC 155 ms
21,220 KB
testcase_10 AC 246 ms
24,340 KB
testcase_11 AC 284 ms
30,892 KB
testcase_12 AC 348 ms
34,988 KB
testcase_13 AC 577 ms
38,760 KB
testcase_14 AC 666 ms
48,640 KB
testcase_15 AC 921 ms
48,336 KB
testcase_16 AC 1,055 ms
64,432 KB
testcase_17 AC 1,152 ms
82,140 KB
testcase_18 AC 1,287 ms
74,136 KB
testcase_19 AC 1,313 ms
74,080 KB
testcase_20 AC 1,463 ms
44,384 KB
testcase_21 AC 1,367 ms
69,168 KB
testcase_22 TLE -
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 = 0
    for v in rtoxy.values():
        if len(v) == 1: continue
        # 凸包を作る
        cnv = ConvexHull(v)
        # 最大距離を求める
        ans = max(ans, RotatingCalipers(cnv) + 2)
    print(ans)

main()
0