結果

問題 No.14 最小公倍数ソート
ユーザー ふーらくたるふーらくたる
提出日時 2018-01-05 00:10:19
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,510 bytes
コンパイル時間 574 ms
コンパイル使用メモリ 67,552 KB
実行使用メモリ 6,752 KB
最終ジャッジ日時 2023-08-24 19:06:56
合計ジャッジ時間 1,884 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
権限があれば一括ダウンロードができます

ソースコード

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++) {
        // a[i]の計算
        if (i > 0) {
            int idx = -1;
            int lcm = -1;
            for (int x = 1; x * x <= ans[i - 1]; x++) {
                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;
}
0