結果

問題 No.14 最小公倍数ソート
ユーザー 👑 colognecologne
提出日時 2022-01-21 12:31:05
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 158 ms / 5,000 ms
コード長 1,291 bytes
コンパイル時間 283 ms
コンパイル使用メモリ 82,176 KB
実行使用メモリ 77,928 KB
最終ジャッジ日時 2024-05-04 01:15:24
合計ジャッジ時間 4,059 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 47 ms
58,240 KB
testcase_01 AC 45 ms
57,600 KB
testcase_02 AC 47 ms
58,752 KB
testcase_03 AC 85 ms
75,904 KB
testcase_04 AC 153 ms
77,580 KB
testcase_05 AC 132 ms
77,260 KB
testcase_06 AC 135 ms
76,928 KB
testcase_07 AC 151 ms
77,780 KB
testcase_08 AC 157 ms
77,484 KB
testcase_09 AC 152 ms
77,568 KB
testcase_10 AC 158 ms
77,824 KB
testcase_11 AC 157 ms
77,816 KB
testcase_12 AC 153 ms
77,912 KB
testcase_13 AC 148 ms
77,920 KB
testcase_14 AC 149 ms
77,416 KB
testcase_15 AC 153 ms
77,656 KB
testcase_16 AC 138 ms
76,928 KB
testcase_17 AC 140 ms
77,624 KB
testcase_18 AC 124 ms
77,928 KB
testcase_19 AC 152 ms
77,560 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 += [ret]*cnt[ret]
        cnt[ret] = 0

    print(*ans, sep=' ')


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