結果

問題 No.14 最小公倍数ソート
ユーザー imulanimulan
提出日時 2018-01-22 18:29:17
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 58 ms / 5,000 ms
コード長 1,527 bytes
コンパイル時間 1,745 ms
コンパイル使用メモリ 174,088 KB
実行使用メモリ 8,452 KB
最終ジャッジ日時 2023-08-26 21:57:11
合計ジャッジ時間 3,329 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 6 ms
4,380 KB
testcase_04 AC 58 ms
8,452 KB
testcase_05 AC 30 ms
6,368 KB
testcase_06 AC 34 ms
6,644 KB
testcase_07 AC 39 ms
7,148 KB
testcase_08 AC 45 ms
7,732 KB
testcase_09 AC 53 ms
8,224 KB
testcase_10 AC 51 ms
8,100 KB
testcase_11 AC 54 ms
8,272 KB
testcase_12 AC 55 ms
8,196 KB
testcase_13 AC 52 ms
8,308 KB
testcase_14 AC 54 ms
8,156 KB
testcase_15 AC 58 ms
8,332 KB
testcase_16 AC 32 ms
6,556 KB
testcase_17 AC 26 ms
6,084 KB
testcase_18 AC 17 ms
5,364 KB
testcase_19 AC 43 ms
7,356 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define fi first
#define se second
#define dbg(x) cout<<#x" = "<<((x))<<endl
template<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<"("<<p.fi<<","<<p.se<<")";return o;}
template<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<"[";for(T t:v){o<<t<<",";}o<<"]";return o;}

int lcm(int x, int y){
    return x/__gcd(x,y)*y;
}

vector<int> divisor(int n){
    vector<int> ret;
    for(int i=1; i*i<=n; ++i){
        if(n%i==0){
            ret.pb(i);
            if(n/i!=i) ret.pb(n/i);
        }
    }
    sort(all(ret));
    return ret;
}

const int N = 10010;
multiset<int> d[N];

int main(){
    int n;
    cin >>n;
    vector<int> a(n);
    rep(i,n) cin >>a[i];

    for(int i=1; i<n; ++i){
        for(int j:divisor(a[i])) d[j].insert(a[i]);
    }

    vector<int> ans;
    ans.pb(a[0]);
    for(int i=1; i<n; ++i){
        int m = N*N;
        int nx = N;

        int now = ans.back();
        for(int j:divisor(now))if(!d[j].empty()){
            int tmp = *d[j].begin();

            int L = lcm(now,tmp);
            if(L<m || (L==m && tmp<nx)){
                m = lcm(now,tmp);
                nx = tmp;
            }
        }

        assert(nx!=N);
        for(int j:divisor(nx)) d[j].erase(d[j].find(nx));
        ans.pb(nx);
    }

    rep(i,n) cout << ans[i] << " \n"[i==n-1];
    return 0;
}
0