結果

問題 No.14 最小公倍数ソート
ユーザー face4face4
提出日時 2019-02-21 17:50:57
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2,905 ms / 5,000 ms
コード長 726 bytes
コンパイル時間 715 ms
コンパイル使用メモリ 75,312 KB
実行使用メモリ 4,508 KB
最終ジャッジ日時 2023-08-12 06:49:43
合計ジャッジ時間 34,693 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 47 ms
4,380 KB
testcase_04 AC 2,905 ms
4,376 KB
testcase_05 AC 952 ms
4,380 KB
testcase_06 AC 1,176 ms
4,380 KB
testcase_07 AC 1,514 ms
4,376 KB
testcase_08 AC 2,015 ms
4,380 KB
testcase_09 AC 2,713 ms
4,376 KB
testcase_10 AC 2,698 ms
4,376 KB
testcase_11 AC 2,709 ms
4,376 KB
testcase_12 AC 2,836 ms
4,376 KB
testcase_13 AC 2,857 ms
4,376 KB
testcase_14 AC 2,766 ms
4,508 KB
testcase_15 AC 2,875 ms
4,380 KB
testcase_16 AC 1,204 ms
4,376 KB
testcase_17 AC 893 ms
4,380 KB
testcase_18 AC 429 ms
4,376 KB
testcase_19 AC 1,917 ms
4,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