結果

問題 No.14 最小公倍数ソート
ユーザー rpy3cpprpy3cpp
提出日時 2016-02-14 21:10:19
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 4,712 ms / 5,000 ms
コード長 1,034 bytes
コンパイル時間 2,026 ms
コンパイル使用メモリ 62,568 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-22 05:22:19
合計ジャッジ時間 53,056 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 1 ms
4,348 KB
testcase_03 AC 50 ms
4,348 KB
testcase_04 AC 4,712 ms
4,348 KB
testcase_05 AC 1,581 ms
4,348 KB
testcase_06 AC 1,848 ms
4,348 KB
testcase_07 AC 2,416 ms
4,348 KB
testcase_08 AC 3,177 ms
4,348 KB
testcase_09 AC 4,300 ms
4,348 KB
testcase_10 AC 4,233 ms
4,348 KB
testcase_11 AC 4,377 ms
4,348 KB
testcase_12 AC 4,507 ms
4,348 KB
testcase_13 AC 4,558 ms
4,348 KB
testcase_14 AC 4,432 ms
4,348 KB
testcase_15 AC 4,576 ms
4,348 KB
testcase_16 AC 1,701 ms
4,348 KB
testcase_17 AC 1,215 ms
4,348 KB
testcase_18 AC 541 ms
4,348 KB
testcase_19 AC 2,871 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <utility>
using namespace std;

int lcm(int a, int b){
    int ab = a * b;
    int tmp = 0;
    while (b){
        tmp = a % b;
        a = b;
        b = tmp;
    }
    return ab / a;
}

void lcm_sort(vector<int> & a){
    for (int i = 0; i < a.size() - 2; ++i){
        int ai = a[i];
        int min_lcm = 100000001;
        int ak = 10001;
        int k = 0;
        for (int j = i + 1; j != a.size(); ++j){
            int aj = a[j];
            int lcmij = lcm(ai, aj);
            if (lcmij < min_lcm){
                min_lcm = lcmij;
                ak = aj;
                k = j;
            }else if (lcmij == min_lcm and aj < ak){
                ak = aj;
                k = j;
            } 
        }
        swap(a[i + 1], a[k]);
    }
}

int main()
{
    int n = 0;
    cin >> n;
    vector<int> a(n, 0);
    for (auto & ai : a) cin >> ai;
    lcm_sort(a);
    for (int i = 0; i != n; ++i) cout << a[i] << ((i == n-1) ? "\n" : " ");
    return 0;
}
0