結果

問題 No.14 最小公倍数ソート
ユーザー Navier_BoltzmannNavier_Boltzmann
提出日時 2023-07-02 17:53:41
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 337 ms / 5,000 ms
コード長 1,383 bytes
コンパイル時間 349 ms
コンパイル使用メモリ 87,064 KB
実行使用メモリ 91,020 KB
最終ジャッジ日時 2023-09-23 14:03:01
合計ジャッジ時間 6,836 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 100 ms
73,068 KB
testcase_01 AC 99 ms
72,784 KB
testcase_02 AC 100 ms
72,932 KB
testcase_03 AC 168 ms
82,072 KB
testcase_04 AC 337 ms
90,740 KB
testcase_05 AC 246 ms
86,556 KB
testcase_06 AC 257 ms
87,436 KB
testcase_07 AC 284 ms
88,520 KB
testcase_08 AC 297 ms
89,120 KB
testcase_09 AC 315 ms
90,756 KB
testcase_10 AC 327 ms
90,812 KB
testcase_11 AC 327 ms
91,020 KB
testcase_12 AC 325 ms
90,516 KB
testcase_13 AC 328 ms
90,840 KB
testcase_14 AC 318 ms
90,416 KB
testcase_15 AC 335 ms
90,884 KB
testcase_16 AC 271 ms
88,304 KB
testcase_17 AC 244 ms
87,752 KB
testcase_18 AC 216 ms
85,692 KB
testcase_19 AC 295 ms
89,152 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import *
from itertools import *
from functools import *
from heapq import *
import sys,math
input = sys.stdin.readline

N = int(input())
A = list(map(int,input().split()))
def md(n):
    lower_divisors , upper_divisors = [], []
    i = 1
    while i*i <= n:
        if n % i == 0:
            lower_divisors.append(i)
            if i != n // i:
                upper_divisors.append(n//i)
        i += 1
    return lower_divisors + upper_divisors[::-1]
    
X = defaultdict(list)
for i,a in enumerate(A):
    for p in md(a):
        X[p].append((a,i))

for k in X.keys():
    X[k].sort()
    X[k] = deque(X[k])

called = [False]*N
ans = [0]
called[0]=True
for _ in range(N-1):
    
    I = A[ans[-1]]
    
    idx = -1
    G = (1<<60)
    jval = (1<<60)
    
    for p in md(I):
        
        if len(X[p])==0:
            continue
        
        val,jdx = X[p].popleft()
        while called[jdx]:
            if len(X[p])==0:
                break
            val,jdx = X[p].popleft()
        if called[jdx]:
            continue
        X[p].appendleft((val,jdx))
        tmp = I*val//math.gcd(I,val)
        if tmp<G:
            G = tmp
            idx = jdx
            jval = val
        elif tmp==G:
            if val<jval:
                jval = val
                idx = jdx
    called[idx]=True

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