結果

問題 No.14 最小公倍数ソート
ユーザー 👑 colognecologne
提出日時 2022-01-21 12:59:09
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 158 ms / 5,000 ms
コード長 1,286 bytes
コンパイル時間 1,939 ms
コンパイル使用メモリ 86,788 KB
実行使用メモリ 79,316 KB
最終ジャッジ日時 2023-08-16 17:59:08
合計ジャッジ時間 5,757 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 65 ms
75,780 KB
testcase_01 AC 62 ms
75,072 KB
testcase_02 AC 66 ms
75,720 KB
testcase_03 AC 91 ms
77,336 KB
testcase_04 AC 156 ms
79,140 KB
testcase_05 AC 138 ms
78,680 KB
testcase_06 AC 140 ms
78,456 KB
testcase_07 AC 146 ms
78,988 KB
testcase_08 AC 152 ms
79,212 KB
testcase_09 AC 157 ms
78,820 KB
testcase_10 AC 158 ms
78,940 KB
testcase_11 AC 158 ms
79,316 KB
testcase_12 AC 154 ms
78,736 KB
testcase_13 AC 156 ms
78,588 KB
testcase_14 AC 155 ms
78,688 KB
testcase_15 AC 155 ms
78,908 KB
testcase_16 AC 143 ms
79,124 KB
testcase_17 AC 141 ms
79,240 KB
testcase_18 AC 125 ms
78,864 KB
testcase_19 AC 154 ms
78,892 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import itertools
import sys

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.append(ret)
        cnt[ret] -= 1

    print(*ans, sep=' ')


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