結果

問題 No.14 最小公倍数ソート
ユーザー koyoprokoyopro
提出日時 2015-09-28 17:50:48
言語 C++11
(gcc 11.4.0)
結果
TLE  
実行時間 -
コード長 1,687 bytes
コンパイル時間 618 ms
コンパイル使用メモリ 75,976 KB
実行使用メモリ 8,760 KB
最終ジャッジ日時 2023-09-26 17:12:57
合計ジャッジ時間 7,319 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,500 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 77 ms
4,380 KB
testcase_04 TLE -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <sstream>
#include <math.h>
#include <vector>
#include <array>
#include <algorithm>
#include <queue>
#include <numeric>
#include <map>
#include <set>
#include <bitset>

using namespace std;
#define int long long
#define FOR(i, a, b) for(int i=(a);i<(b);i++)
#define RFOR(i, a, b) for(int i=(b-1);i>=(a);i--)
#define REP(i, n) for(int i=0; i<(n); i++)
#define RREP(i, n) for(int i=(n-1); i>=0; i--)
#define ALL(a) (a).begin(),(a).end()
#define UNIQUE_SORT(l) sort(ALL(l)); l.erase(unique(ALL(l)), l.end());
#define CONTAIN(a, b) find(ALL(a), (b)) != (a).end()
#define SUM(a) accumulate(ALL(a), 0)
#define array2(type, x, y) array<array<type, y>, x>
#define vector2(type) vector<vector<type> >
#define out(...) printf(__VA_ARGS__)

typedef pair<int, int> pos;
int pos::*x = &pos::first;
int pos::*y = &pos::second;

int dxy[] = {0, 1, 0, -1, 0};

/*================================*/

int N;

int gcd(int a, int b) {
    while (a % b != 0) {
        int tmp = a % b;
        a = b;
        b = tmp;
    }
    return b;
}

signed main()
{
    cin >> N;
    vector<int> A(N);
    REP(i,N) cin >> A[i];

    REP(i,N-1) {
        int a = A[i];
        int minlcm = 1e15;
        int minlcmj = i+1;
        FOR(j,i+1,N) {
            int lcm = a * A[j] / gcd(a, A[j]);
            if (lcm < minlcm) {
                minlcm = lcm;
                minlcmj = j;
            } else if (lcm == minlcm && A[j] < A[minlcmj]) {
                minlcmj = j;
            }
        }
        if (i+1 != minlcmj) {
            swap(A[i+1], A[minlcmj]);
        }
    }

    REP(i,N) {
        if (i) cout << " ";
        cout << A[i];
    }
    cout << endl;

    return 0;
}
0