結果

問題 No.14 最小公倍数ソート
ユーザー face4face4
提出日時 2019-02-21 18:36:30
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 72 ms / 5,000 ms
コード長 1,248 bytes
コンパイル時間 936 ms
コンパイル使用メモリ 82,924 KB
実行使用メモリ 9,344 KB
最終ジャッジ日時 2024-04-29 22:19:08
合計ジャッジ時間 2,741 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 3 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 7 ms
5,376 KB
testcase_04 AC 72 ms
9,344 KB
testcase_05 AC 38 ms
7,040 KB
testcase_06 AC 38 ms
7,296 KB
testcase_07 AC 46 ms
7,808 KB
testcase_08 AC 52 ms
8,448 KB
testcase_09 AC 62 ms
8,832 KB
testcase_10 AC 66 ms
9,088 KB
testcase_11 AC 65 ms
9,344 KB
testcase_12 AC 61 ms
9,088 KB
testcase_13 AC 59 ms
9,216 KB
testcase_14 AC 57 ms
9,216 KB
testcase_15 AC 59 ms
9,216 KB
testcase_16 AC 33 ms
7,168 KB
testcase_17 AC 27 ms
6,656 KB
testcase_18 AC 19 ms
5,632 KB
testcase_19 AC 46 ms
8,192 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