結果

問題 No.14 最小公倍数ソート
ユーザー ふーらくたるふーらくたる
提出日時 2018-01-05 00:19:51
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 24 ms / 5,000 ms
コード長 1,632 bytes
コンパイル時間 1,052 ms
コンパイル使用メモリ 69,676 KB
実行使用メモリ 6,804 KB
最終ジャッジ日時 2023-08-24 19:08:10
合計ジャッジ時間 2,118 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 4 ms
4,376 KB
testcase_04 AC 24 ms
6,780 KB
testcase_05 AC 14 ms
5,516 KB
testcase_06 AC 15 ms
5,484 KB
testcase_07 AC 18 ms
5,760 KB
testcase_08 AC 19 ms
6,376 KB
testcase_09 AC 22 ms
6,548 KB
testcase_10 AC 22 ms
6,472 KB
testcase_11 AC 22 ms
6,496 KB
testcase_12 AC 22 ms
6,480 KB
testcase_13 AC 23 ms
6,800 KB
testcase_14 AC 23 ms
6,504 KB
testcase_15 AC 23 ms
6,804 KB
testcase_16 AC 15 ms
5,432 KB
testcase_17 AC 12 ms
5,092 KB
testcase_18 AC 9 ms
4,580 KB
testcase_19 AC 18 ms
6,172 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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++) {
        // ans[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 idx2 = lst[n].front();
                    int lcm2 = a[idx2] * ans[i - 1] / n;
                    if (idx < 0 or make_pair(lcm2, a[idx2]) < make_pair(lcm, a[idx])) {
                        idx = idx2;
                        lcm = lcm2;
                    }
                };
                update(x);
                update(y);
            }
            ans[i] = a[idx];
            used[idx] = true;
        }
        cout << ans[i] << " ";
    }
    cout << endl;
    
    return 0;
}
0