結果
問題 |
No.14 最小公倍数ソート
|
ユーザー |
![]() |
提出日時 | 2020-03-25 00:43:52 |
言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) |
結果 |
AC
|
実行時間 | 281 ms / 5,000 ms |
コード長 | 1,839 bytes |
コンパイル時間 | 374 ms |
コンパイル使用メモリ | 12,544 KB |
実行使用メモリ | 20,096 KB |
最終ジャッジ日時 | 2025-01-01 19:04:43 |
合計ジャッジ時間 | 5,370 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 20 |
ソースコード
#!/usr/bin/ python3.8 import sys read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines from heapq import heapify, heappop from collections import defaultdict N, *A = map(int, read().split()) class RemovableHeap(): def __init__(self, data): self.n_elem = len(data) self.data = data heapify(self.data) self.counter = defaultdict(int) for x in data: self.counter[x] += 1 def top(self): while True: x = self.data[0] if not self.counter[x]: heappop(self.data) continue return x def push(self, x): self.counter[x] += 1 heappush(self.data, x) self.n_elem += 1 def pop(self): while True: x = heappop(self.data) if self.counter[x]: self.counter[x] -= 1 self.n_elem -= 1 return x def remove(self, x): self.counter[x] -= 1 self.n_elem -= 1 def empty(self): return self.n_elem == 0 U = 10 ** 4 div = [[] for _ in range(U + 1)] for d in range(1, U + 1): for i in range(d, U + 1, d): div[i].append(d) INF = 10 ** 9 multiples = [[INF] for _ in range(U + 1)] for x in A[1:]: for d in div[x]: multiples[d].append(x) for i in range(1, U + 1): multiples[i] = RemovableHeap(multiples[i]) answer = [A[0]] n = A[0] for _ in range(N - 1): best_lcm = INF best_x = 0 for d in div[n]: x = multiples[d].top() lcm = n * x // d if best_lcm > lcm: best_lcm = lcm best_x = x n = best_x answer.append(n) for d in div[n]: multiples[d].remove(n) print(*answer) # %% # import numpy as np # from numba import njit # %%