結果

問題 No.14 最小公倍数ソート
ユーザー legosukelegosuke
提出日時 2021-08-08 05:18:02
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 68 ms / 5,000 ms
コード長 1,109 bytes
コンパイル時間 2,399 ms
コンパイル使用メモリ 213,128 KB
実行使用メモリ 9,404 KB
最終ジャッジ日時 2023-10-19 04:53:58
合計ジャッジ時間 4,207 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
4,408 KB
testcase_01 AC 2 ms
4,408 KB
testcase_02 AC 2 ms
4,408 KB
testcase_03 AC 6 ms
4,908 KB
testcase_04 AC 68 ms
9,404 KB
testcase_05 AC 33 ms
7,240 KB
testcase_06 AC 37 ms
7,508 KB
testcase_07 AC 44 ms
7,976 KB
testcase_08 AC 54 ms
8,536 KB
testcase_09 AC 65 ms
9,108 KB
testcase_10 AC 64 ms
9,068 KB
testcase_11 AC 68 ms
9,260 KB
testcase_12 AC 67 ms
9,240 KB
testcase_13 AC 67 ms
9,292 KB
testcase_14 AC 64 ms
9,208 KB
testcase_15 AC 68 ms
9,356 KB
testcase_16 AC 36 ms
7,448 KB
testcase_17 AC 28 ms
6,928 KB
testcase_18 AC 18 ms
6,088 KB
testcase_19 AC 48 ms
8,336 KB
権限があれば一括ダウンロードができます

ソースコード

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});
            if (S[div].empty()) continue;
            auto v = *begin(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