結果

問題 No.14 最小公倍数ソート
ユーザー msm1993msm1993
提出日時 2019-04-16 13:10:29
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 66 ms / 5,000 ms
コード長 1,248 bytes
コンパイル時間 754 ms
コンパイル使用メモリ 73,964 KB
実行使用メモリ 9,460 KB
最終ジャッジ日時 2023-10-22 07:01:21
合計ジャッジ時間 3,331 ms
ジャッジサーバーID
(参考情報)
judge9 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 6 ms
4,616 KB
testcase_04 AC 66 ms
9,460 KB
testcase_05 AC 33 ms
7,144 KB
testcase_06 AC 36 ms
7,416 KB
testcase_07 AC 42 ms
7,916 KB
testcase_08 AC 49 ms
8,540 KB
testcase_09 AC 59 ms
9,152 KB
testcase_10 AC 60 ms
9,104 KB
testcase_11 AC 63 ms
9,304 KB
testcase_12 AC 63 ms
9,288 KB
testcase_13 AC 65 ms
9,348 KB
testcase_14 AC 61 ms
9,252 KB
testcase_15 AC 62 ms
9,404 KB
testcase_16 AC 35 ms
7,344 KB
testcase_17 AC 28 ms
6,788 KB
testcase_18 AC 18 ms
5,880 KB
testcase_19 AC 49 ms
8,284 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<set>
#include<vector>
using namespace std;

int gcd(int a, int b){
    return b == 0 ? a : gcd(b, a%b);
}

int main(){
    int n;
    cin >> n;
    vector<int> v(n), divs[n];
    set<pair<int,int>> s[10001];
    for(int i = 0; i < n; i++){
        cin >> v[i];
        for(int j = 1; j*j <= v[i]; j++){
            if(v[i]%j)  continue;
            divs[i].push_back(j);
            s[j].insert({v[i], i});
            if(j*j != v[i]){
                divs[i].push_back(v[i]/j);
                s[v[i]/j].insert({v[i], i});
            }
        }
    }
    vector<int> ans;
    int use = 0;
    for(int i = 0; i < n; i++){
        ans.push_back(v[use]);
        int take = -1;
        pair<int,int> tmp(1<<30,10001); // gcd, val
        for(int d : divs[use]){
            s[d].erase({v[use], use});
            if(s[d].empty())    continue;
            pair<int,int> cand = *(s[d].begin());
            int lcm = cand.first*v[use]/gcd(cand.first, v[use]);
            pair<int,int> q(lcm, cand.first);
            if(q < tmp){
                tmp = q;
                take = cand.second;
            }
        }
        use = take;
    }
    for(int i = 0; i < n; i++)  cout << ans[i] << " \n"[i==n-1];
    return 0;
}
0