結果

問題 No.14 最小公倍数ソート
ユーザー szkhtsszkhts
提出日時 2021-01-31 09:49:05
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 320 ms / 5,000 ms
コード長 797 bytes
コンパイル時間 180 ms
コンパイル使用メモリ 10,888 KB
実行使用メモリ 18,656 KB
最終ジャッジ日時 2023-09-11 04:46:02
合計ジャッジ時間 6,015 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 43 ms
10,804 KB
testcase_01 AC 43 ms
10,628 KB
testcase_02 AC 43 ms
10,808 KB
testcase_03 AC 67 ms
11,528 KB
testcase_04 AC 320 ms
18,608 KB
testcase_05 AC 188 ms
15,392 KB
testcase_06 AC 204 ms
15,576 KB
testcase_07 AC 226 ms
16,428 KB
testcase_08 AC 264 ms
17,288 KB
testcase_09 AC 298 ms
18,224 KB
testcase_10 AC 297 ms
18,008 KB
testcase_11 AC 307 ms
18,356 KB
testcase_12 AC 318 ms
18,396 KB
testcase_13 AC 308 ms
18,308 KB
testcase_14 AC 307 ms
18,392 KB
testcase_15 AC 317 ms
18,656 KB
testcase_16 AC 202 ms
15,736 KB
testcase_17 AC 171 ms
14,820 KB
testcase_18 AC 128 ms
13,524 KB
testcase_19 AC 257 ms
17,020 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def gcd(a, b):
    if b == 0:
        return a
    else:
        return gcd(b, a % b)

u = 10 ** 4
dv = [[] for i in range(u + 1)]
ds = [[] for i in range(u + 1)]
for i in range(1, u + 1):
  for j in range(i, u + 1, i):
    dv[j].append(i)

n = int(input())
a = list(map(int, input().split()))
for i, x in enumerate(a):
  for d in dv[x]:
    ds[d].append((x, i))
for e in ds:
  e.sort()
  e.reverse()

mark = [0] * n
ret, last = [a[0]], 0
for i in range(n - 1):
  mark[last] = 1
  r, x = 1 << 30, -1
  for d in dv[a[last]]:
    while len(ds[d]) and mark[ds[d][-1][1]]:
      ds[d].pop(-1)
    if not len(ds[d]):
      continue
    y = ds[d][-1][1]
    t = a[y] * a[last] / gcd(a[y], a[last])
    if t < r or (t == r and a[y] < a[x]):
      r, x = t, y
  last = x
  ret.append(a[x])
  
print(*ret)
0