結果

問題 No.14 最小公倍数ソート
ユーザー 👑 colognecologne
提出日時 2022-01-21 12:28:41
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 152 ms / 5,000 ms
コード長 1,309 bytes
コンパイル時間 154 ms
コンパイル使用メモリ 82,048 KB
実行使用メモリ 78,208 KB
最終ジャッジ日時 2024-05-04 01:13:09
合計ジャッジ時間 3,378 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 46 ms
58,368 KB
testcase_01 AC 45 ms
57,088 KB
testcase_02 AC 48 ms
57,856 KB
testcase_03 AC 94 ms
75,904 KB
testcase_04 AC 140 ms
77,836 KB
testcase_05 AC 126 ms
77,388 KB
testcase_06 AC 124 ms
77,312 KB
testcase_07 AC 138 ms
78,044 KB
testcase_08 AC 152 ms
77,300 KB
testcase_09 AC 152 ms
77,828 KB
testcase_10 AC 144 ms
78,208 KB
testcase_11 AC 144 ms
77,684 KB
testcase_12 AC 140 ms
77,784 KB
testcase_13 AC 138 ms
77,540 KB
testcase_14 AC 140 ms
77,676 KB
testcase_15 AC 139 ms
77,524 KB
testcase_16 AC 125 ms
77,440 KB
testcase_17 AC 126 ms
77,508 KB
testcase_18 AC 115 ms
77,824 KB
testcase_19 AC 137 ms
77,944 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