結果

問題 No.14 最小公倍数ソート
ユーザー 👑 colognecologne
提出日時 2022-01-21 12:28:41
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 189 ms / 5,000 ms
コード長 1,309 bytes
コンパイル時間 295 ms
コンパイル使用メモリ 87,048 KB
実行使用メモリ 79,456 KB
最終ジャッジ日時 2023-08-16 17:24:34
合計ジャッジ時間 4,697 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 77 ms
75,780 KB
testcase_01 AC 76 ms
75,152 KB
testcase_02 AC 79 ms
75,736 KB
testcase_03 AC 110 ms
77,188 KB
testcase_04 AC 189 ms
79,040 KB
testcase_05 AC 157 ms
78,836 KB
testcase_06 AC 155 ms
78,424 KB
testcase_07 AC 171 ms
79,456 KB
testcase_08 AC 171 ms
78,984 KB
testcase_09 AC 174 ms
79,408 KB
testcase_10 AC 172 ms
78,672 KB
testcase_11 AC 173 ms
78,816 KB
testcase_12 AC 171 ms
78,660 KB
testcase_13 AC 172 ms
78,708 KB
testcase_14 AC 173 ms
79,356 KB
testcase_15 AC 175 ms
79,392 KB
testcase_16 AC 158 ms
78,912 KB
testcase_17 AC 159 ms
78,556 KB
testcase_18 AC 146 ms
79,080 KB
testcase_19 AC 171 ms
78,796 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import itertools
import sys
import itertools

input = sys.stdin.readline


def main():
    N = int(input())
    *A, = map(int, input().split())

    MAX = 10000
    cnt = [0]*(MAX+1)
    for a in A[1:]:
        cnt[a] += 1

    ptr = list(range(MAX+1))

    def getnxt(x):
        while ptr[x] <= MAX and cnt[ptr[x]] == 0:
            ptr[x] += x

        return ptr[x] if ptr[x] <= MAX else None

    def getmin(v):
        maxv = ans = MAX * MAX
        for i in itertools.count(1):
            if i*i > v:
                return ans

            if v % i == 0:

                n = getnxt(i)
                if n is not None:
                    l = n * (v // i)
                    if maxv > l:
                        maxv = l
                        ans = n
                    elif maxv == l:
                        ans = min(ans, n)

                n = getnxt(v // i)
                if n is not None:
                    l = n * i
                    if maxv > l:
                        maxv = l
                        ans = n
                    elif maxv == l:
                        ans = min(ans, n)


    ans = [A[0]]
    while len(ans) != N:
        ret = getmin(ans[-1])
        ans += [ret]*cnt[ret]
        cnt[ret] = 0

    print(*ans, sep=' ')


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