結果

問題 No.14 最小公倍数ソート
ユーザー tktk_snsntktk_snsn
提出日時 2021-03-28 18:43:48
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 261 ms / 5,000 ms
コード長 912 bytes
コンパイル時間 804 ms
コンパイル使用メモリ 10,880 KB
実行使用メモリ 19,736 KB
最終ジャッジ日時 2023-08-19 15:44:51
合計ジャッジ時間 4,897 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 18 ms
8,904 KB
testcase_01 AC 17 ms
8,944 KB
testcase_02 AC 18 ms
8,900 KB
testcase_03 AC 38 ms
10,276 KB
testcase_04 AC 252 ms
19,588 KB
testcase_05 AC 137 ms
15,168 KB
testcase_06 AC 151 ms
15,752 KB
testcase_07 AC 181 ms
16,696 KB
testcase_08 AC 204 ms
17,804 KB
testcase_09 AC 242 ms
19,212 KB
testcase_10 AC 241 ms
19,296 KB
testcase_11 AC 261 ms
19,340 KB
testcase_12 AC 244 ms
19,392 KB
testcase_13 AC 258 ms
19,616 KB
testcase_14 AC 248 ms
19,292 KB
testcase_15 AC 252 ms
19,736 KB
testcase_16 AC 173 ms
15,772 KB
testcase_17 AC 126 ms
14,616 KB
testcase_18 AC 85 ms
12,588 KB
testcase_19 AC 193 ms
17,444 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict
import heapq
from math import gcd


def getdiv(n):
    res = set()
    for i in range(1, int(n ** 0.5) + 1):
        if n % i == 0:
            res.add(i)
            res.add(n // i)
    return tuple(sorted(res))


N = int(input())
A = list(map(int, input().split()))
div = [getdiv(a) for a in A]

d = defaultdict(list)
for i, a in enumerate(A):
    for x in div[i]:
        heapq.heappush(d[x], (a, i))

seen = [0] * N
seen[0] = 1
ans = [0]

a = A[0]
now = 0
for _ in range(N - 1):
    min_val = 10 ** 18
    min_x = -1
    for x in div[now]:
        while d[x] and seen[d[x][0][1]]:
            heapq.heappop(d[x])
        if not d[x]:
            continue
        tmp_val = d[x][0][0]//x
        if tmp_val < min_val:
            min_val = tmp_val
            min_x = x
    a, now = heapq.heappop(d[min_x])
    ans.append(now)
    seen[now] = 1

print(*[A[i] for i in ans])
0