結果

問題 No.14 最小公倍数ソート
ユーザー maine_honzukimaine_honzuki
提出日時 2020-05-04 17:29:15
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 55 ms / 5,000 ms
コード長 1,211 bytes
コンパイル時間 1,628 ms
コンパイル使用メモリ 172,912 KB
実行使用メモリ 9,228 KB
最終ジャッジ日時 2023-09-06 23:38:43
合計ジャッジ時間 3,766 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 7 ms
4,676 KB
testcase_01 AC 7 ms
4,868 KB
testcase_02 AC 7 ms
4,676 KB
testcase_03 AC 10 ms
5,116 KB
testcase_04 AC 55 ms
9,204 KB
testcase_05 AC 29 ms
7,296 KB
testcase_06 AC 32 ms
7,476 KB
testcase_07 AC 36 ms
7,916 KB
testcase_08 AC 42 ms
8,388 KB
testcase_09 AC 48 ms
8,948 KB
testcase_10 AC 49 ms
8,972 KB
testcase_11 AC 51 ms
9,088 KB
testcase_12 AC 50 ms
9,116 KB
testcase_13 AC 51 ms
9,116 KB
testcase_14 AC 52 ms
9,228 KB
testcase_15 AC 52 ms
9,160 KB
testcase_16 AC 31 ms
7,428 KB
testcase_17 AC 27 ms
7,288 KB
testcase_18 AC 18 ms
6,188 KB
testcase_19 AC 40 ms
8,336 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

int main() {
    int N, A[10010];
    cin >> N;
    for (int i = 0; i < N; i++)
        cin >> A[i];

    vector<int> yakusuu[10010];
    multiset<int> S[10010];
    for (int i = 1; i <= 10000; i++) {
        for (int x = 1; x * x <= i; x++) {
            if (i % x == 0) {
                yakusuu[i].push_back(x);
                if (x * x != i)
                    yakusuu[i].push_back(i / x);
            }
        }
    }

    for (int i = 0; i < N; i++) {
        for (auto& x : yakusuu[A[i]])
            S[x].insert(A[i]);
    }

    vector<int> ans;
    ans.push_back(A[0]);
    int nxt = A[0];
    while (ans.size() != N) {
        int MIN = 1e8 + 10, cur = 1e5;

        for (auto& x : yakusuu[nxt]) {
            S[x].erase(S[x].find(nxt));
            if (!S[x].empty()) {
                if (MIN > *S[x].begin() * nxt / x || (MIN == *S[x].begin() * nxt / x) && *S[x].begin() < cur) {
                    cur = *S[x].begin();
                    MIN = *S[x].begin() * nxt / x;
                }
            }
        }

        ans.push_back(cur);
        nxt = cur;
    }

    for (auto& x : ans) {
        cout << x << " ";
    }
    cout << endl;
}
0