結果

問題 No.14 最小公倍数ソート
ユーザー face4face4
提出日時 2019-02-21 17:50:57
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2,918 ms / 5,000 ms
コード長 726 bytes
コンパイル時間 720 ms
コンパイル使用メモリ 75,904 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-29 21:20:34
合計ジャッジ時間 34,380 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 48 ms
5,376 KB
testcase_04 AC 2,918 ms
5,376 KB
testcase_05 AC 977 ms
5,376 KB
testcase_06 AC 1,178 ms
5,376 KB
testcase_07 AC 1,539 ms
5,376 KB
testcase_08 AC 2,031 ms
5,376 KB
testcase_09 AC 2,710 ms
5,376 KB
testcase_10 AC 2,695 ms
5,376 KB
testcase_11 AC 2,723 ms
5,376 KB
testcase_12 AC 2,885 ms
5,376 KB
testcase_13 AC 2,853 ms
5,376 KB
testcase_14 AC 2,766 ms
5,376 KB
testcase_15 AC 2,857 ms
5,376 KB
testcase_16 AC 1,189 ms
5,376 KB
testcase_17 AC 886 ms
5,376 KB
testcase_18 AC 421 ms
5,376 KB
testcase_19 AC 1,896 ms
5,376 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;
    int now;
    cin >> now;
    vector<int> v(1, now);
    multiset<int> s; int x;
    for(int i = 1; i < n; i++)  cin >> x, s.insert(x);
    while(!s.empty()){
        int tmp = 1<<30, take;
        for(int x : s){
            if(x > tmp) break;
            int lcm = now*x/gcd(now,x);
            if(tmp > lcm){
                tmp = lcm;
                take = x;
            }
        }
        v.push_back(take);
        s.erase(s.find(take));
        now = take;
    }
    for(int i = 0; i < n; i++)  cout << v[i] << " \n"[i==n-1];
    return 0;
}
0