結果

問題 No.14 最小公倍数ソート
ユーザー legosukelegosuke
提出日時 2021-08-08 05:14:13
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,100 bytes
コンパイル時間 3,289 ms
コンパイル使用メモリ 213,052 KB
実行使用メモリ 13,484 KB
最終ジャッジ日時 2023-10-19 04:50:36
合計ジャッジ時間 9,477 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
4,348 KB
testcase_01 AC 3 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 71 ms
4,848 KB
testcase_04 TLE -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

const int MAX_N = 10000;
const int MAX_A = 10000;
int N;
vector<int> divs[MAX_N];
set<pair<int, int>> S[MAX_A + 1];

int main() {
    int N;
    cin >> N;
    vector<int> a(N);
    for (int i = 0; i < N; ++i) {
        cin >> a[i];
        for (int j = 1; j * j <= a[i]; ++j) {
            if (a[i] % j != 0) continue;
            divs[a[i]].push_back(j);
            S[j].emplace(a[i], i);
            if (j * j != a[i]) {
                divs[a[i]].push_back(a[i] / j);
                S[a[i] / j].emplace(a[i], i);
            }
        }
    }
    int id = 0;
    for (int i = 0; i < N; ++i) {
        cout << a[id] << " \n"[i + 1 == N];
        int minL = 1 << 30, nid = -1;
        for (int div : divs[a[id]]) {
            S[div].erase({a[id], id});
            for (auto v : S[div]) {
                int L = lcm(a[id], v.first);
                if (L < minL || (L == minL && v.first < a[nid])) {
                    minL = L;
                    nid = v.second;
                }
            }
        }
        id = nid;
    }

    return 0;
}
0