結果
問題 | No.14 最小公倍数ソート |
ユーザー | 👑 rin204 |
提出日時 | 2022-09-27 21:25:07 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 343 ms / 5,000 ms |
コード長 | 2,196 bytes |
コンパイル時間 | 337 ms |
コンパイル使用メモリ | 82,048 KB |
実行使用メモリ | 89,344 KB |
最終ジャッジ日時 | 2024-12-22 17:10:59 |
合計ジャッジ時間 | 6,805 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 66 ms
68,352 KB |
testcase_01 | AC | 64 ms
68,096 KB |
testcase_02 | AC | 62 ms
68,352 KB |
testcase_03 | AC | 197 ms
81,720 KB |
testcase_04 | AC | 325 ms
89,344 KB |
testcase_05 | AC | 273 ms
85,308 KB |
testcase_06 | AC | 272 ms
85,120 KB |
testcase_07 | AC | 298 ms
86,148 KB |
testcase_08 | AC | 319 ms
88,008 KB |
testcase_09 | AC | 321 ms
88,520 KB |
testcase_10 | AC | 343 ms
89,088 KB |
testcase_11 | AC | 308 ms
89,024 KB |
testcase_12 | AC | 335 ms
89,088 KB |
testcase_13 | AC | 324 ms
88,208 KB |
testcase_14 | AC | 312 ms
88,704 KB |
testcase_15 | AC | 315 ms
88,832 KB |
testcase_16 | AC | 282 ms
86,316 KB |
testcase_17 | AC | 279 ms
85,776 KB |
testcase_18 | AC | 231 ms
83,496 KB |
testcase_19 | AC | 308 ms
86,952 KB |
ソースコード
import heapq class Heapq: def __init__(self, lst = [], reverse = False): if reverse: self.pm = -1 self.hq = [-l for l in lst] else: self.pm = 1 self.hq = lst[:] heapq.heapify(self.hq) self.tot = sum(lst) self.cnt = {} for l in lst: self.cnt[l * self.pm] = self.cnt.get(l * self.pm, 0) + 1 self.length = len(lst) def __bool__(self): return self.length > 0 def __len__(self): return self.length def __getitem__(self, i): if i == 0: return self.top() else: assert False def push(self, x): self.length += 1 self.cnt[x * self.pm] = self.cnt.get(x * self.pm, 0) + 1 heapq.heappush(self.hq, x * self.pm) self.tot += x def pop(self): if self.length == 0: return None self.length -= 1 ret = heapq.heappop(self.hq) self.tot -= self.pm * ret self.cnt[ret] -= 1 self.delete() return self.pm * ret def top(self): if self.hq: return self.pm * self.hq[0] else: return None def remove(self, x): if self.cnt.get(x * self.pm, 0) == 0: return False self.cnt[x * self.pm] -= 1 self.length -= 1 self.tot -= x self.delete() return True def delete(self): while self.hq and self.cnt.get(self.hq[0], 0) == 0: heapq.heappop(self.hq) n = int(input()) A = list(map(int, input().split())) ma = 10000 div = [[] for _ in range(ma + 1)] for i in range(1, ma + 1): for j in range(i, ma + 1, i): div[j].append(i) hq = [Heapq() for _ in range(ma + 1)] for a in A[1:]: for d in div[a]: hq[d].push(a) a = A[0] ans = [a] for _ in range(n - 1): mi = 1 << 30 x = -1 for d in div[a]: if not hq[d]: continue b = hq[d].top() y = a * b // d if y < mi: mi = y x = b a = x ans.append(a) for d in div[a]: hq[d].remove(a) print(*ans)