結果

問題 No.14 最小公倍数ソート
ユーザー momoyuumomoyuu
提出日時 2023-03-16 15:43:33
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 4,620 ms / 5,000 ms
コード長 717 bytes
コンパイル時間 5,346 ms
コンパイル使用メモリ 245,592 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-18 13:04:28
合計ジャッジ時間 53,787 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 49 ms
4,348 KB
testcase_04 AC 4,620 ms
4,348 KB
testcase_05 AC 1,549 ms
4,348 KB
testcase_06 AC 1,812 ms
4,348 KB
testcase_07 AC 2,362 ms
4,348 KB
testcase_08 AC 3,115 ms
4,348 KB
testcase_09 AC 4,205 ms
4,348 KB
testcase_10 AC 4,136 ms
4,348 KB
testcase_11 AC 4,289 ms
4,348 KB
testcase_12 AC 4,414 ms
4,348 KB
testcase_13 AC 4,472 ms
4,348 KB
testcase_14 AC 4,338 ms
4,348 KB
testcase_15 AC 4,483 ms
4,348 KB
testcase_16 AC 1,666 ms
4,348 KB
testcase_17 AC 1,189 ms
4,348 KB
testcase_18 AC 530 ms
4,348 KB
testcase_19 AC 2,817 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using ll = long long;

int gcd(int a,int b){
    if(a<b) swap(a,b);
    if(b==0) return a;
    return gcd(b,a%b);
}

int main(){
    int n;
    cin>>n;
    vector<int> a(n);
    for(int i = 0;i<n;i++) cin>>a[i];
    for(int i = 0;i<n-1;i++){
        int l = 1e8;
        int ni = -1;
        for(int j = i + 1;j<n;j++){
            int now = a[i] * a[j] / gcd(a[i],a[j]);
            if(now<l){
                l = now;
                ni = j;
            }else if(now==l){
                if(a[ni]>a[j]) ni = j;
            }
        }
        swap(a[i+1],a[ni]);
    }
    for(int i = 0;i<n;i++){
        if(i!=0) cout<<" ";
        cout<<a[i];
    }
    cout<<endl;
}

0