結果
問題 | No.14 最小公倍数ソート |
ユーザー |
![]() |
提出日時 | 2018-01-05 00:13:00 |
言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,561 bytes |
コンパイル時間 | 678 ms |
コンパイル使用メモリ | 65,856 KB |
実行使用メモリ | 6,784 KB |
最終ジャッジ日時 | 2024-12-23 03:57:01 |
合計ジャッジ時間 | 1,759 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 3 WA * 17 |
ソースコード
#include <iostream> #include <vector> #include <list> #include <algorithm> #include <utility> using namespace std; const int MAX_N = 10010; int main() { int N; cin >> N; static int a[MAX_N]; for (int i = 0; i < N; i++) { cin >> a[i]; } sort(a + 1, a + N); static list<int> lst[MAX_N]; for (int i = 0; i < N; i++) { for (int x = 1; x * x <= a[i]; x++) { if (a[i] % x == 0) { lst[x].emplace_back(i); lst[a[i] / x].emplace_back(i); } } } static bool used[MAX_N]; static int ans[MAX_N]; ans[0] = a[0]; used[0] = true; for (int i = 0; i < N; i++) { // a[i]の計算 if (i > 0) { int idx = -1; int lcm = -1; for (int x = 1; x * x <= ans[i - 1]; x++) { if (ans[i - 1] % x != 0) continue; int y = ans[i - 1] / x; auto update = [&](int n) { while (!lst[n].empty() and used[lst[n].front()]) lst[n].pop_front(); if (lst[n].empty()) return; int val = a[lst[n].front()] * ans[i - 1] / n; if (idx < 0 or val < lcm) { idx = lst[n].front(); lcm = val; } }; update(x); update(y); } ans[i] = a[idx]; used[idx] = true; } cout << ans[i] << " "; } cout << endl; return 0; }