結果

問題 No.168 ものさし
ユーザー nisizawanisizawa
提出日時 2017-12-19 10:53:56
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 541 ms / 2,000 ms
コード長 925 bytes
コンパイル時間 80 ms
コンパイル使用メモリ 10,928 KB
実行使用メモリ 81,748 KB
最終ジャッジ日時 2023-08-25 21:07:30
合計ジャッジ時間 5,309 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 138 ms
24,164 KB
testcase_01 AC 16 ms
8,064 KB
testcase_02 AC 17 ms
8,140 KB
testcase_03 AC 16 ms
8,180 KB
testcase_04 AC 16 ms
8,112 KB
testcase_05 AC 16 ms
8,200 KB
testcase_06 AC 16 ms
8,108 KB
testcase_07 AC 16 ms
8,040 KB
testcase_08 AC 16 ms
8,136 KB
testcase_09 AC 20 ms
8,484 KB
testcase_10 AC 30 ms
9,548 KB
testcase_11 AC 122 ms
22,132 KB
testcase_12 AC 392 ms
57,872 KB
testcase_13 AC 532 ms
79,712 KB
testcase_14 AC 527 ms
79,868 KB
testcase_15 AC 16 ms
8,084 KB
testcase_16 AC 18 ms
8,828 KB
testcase_17 AC 24 ms
9,028 KB
testcase_18 AC 39 ms
11,384 KB
testcase_19 AC 503 ms
78,828 KB
testcase_20 AC 531 ms
81,744 KB
testcase_21 AC 541 ms
81,708 KB
testcase_22 AC 524 ms
81,748 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import heapq

n = int(input())
xy_ls = [tuple(map(int, input().split())) for i in range(n)]

def sq_dist(xy1, xy2):
    x1, y1 = xy1
    x2, y2 = xy2
    return (x2 - x1)**2 + (y2 - y1)**2

sq_dist_ls = []
for i in range(n - 1):
    xy1 = xy_ls[i]
    for j in range(i + 1, n):
        xy2 = xy_ls[j]
        heapq.heappush(sq_dist_ls, (sq_dist(xy1, xy2), i, j))

def p_find(memo, p):
    while memo[p] >= 0:
        p = memo[p]
    return p

def p_union(memo, p1, p2):
    p1_root = p_find(memo, p1)
    p2_root = p_find(memo, p2)
    if p1_root != p2_root:
        memo[p1_root] = p2_root

def get_ans(sq_x):
    x = int(sq_x**0.5 // 10 * 10)
    return x + 10 if x**2 < sq_x else x

start = 0
goal = n - 1

memo = [-1] * n
while len(sq_dist_ls) > 0:
    sq_dist, p1, p2 = heapq.heappop(sq_dist_ls)
    p_union(memo, p1, p2)
    if p_find(memo, start) == p_find(memo, goal):
        break

ans = get_ans(sq_dist)
print(ans)
0