結果

問題 No.14 最小公倍数ソート
ユーザー eitahoeitaho
提出日時 2015-02-14 04:22:27
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 4,653 ms / 5,000 ms
コード長 1,383 bytes
コンパイル時間 547 ms
コンパイル使用メモリ 75,536 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-06 00:49:45
合計ジャッジ時間 52,295 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 50 ms
4,376 KB
testcase_04 AC 4,653 ms
4,380 KB
testcase_05 AC 1,565 ms
4,376 KB
testcase_06 AC 1,827 ms
4,376 KB
testcase_07 AC 2,383 ms
4,380 KB
testcase_08 AC 3,138 ms
4,376 KB
testcase_09 AC 4,243 ms
4,380 KB
testcase_10 AC 4,166 ms
4,376 KB
testcase_11 AC 4,329 ms
4,376 KB
testcase_12 AC 4,457 ms
4,380 KB
testcase_13 AC 4,498 ms
4,376 KB
testcase_14 AC 4,371 ms
4,380 KB
testcase_15 AC 4,526 ms
4,376 KB
testcase_16 AC 1,678 ms
4,376 KB
testcase_17 AC 1,198 ms
4,380 KB
testcase_18 AC 534 ms
4,376 KB
testcase_19 AC 2,833 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <cmath>
#include <cassert>
#include <climits>
#include <numeric>
#include <functional>
#include <time.h>

using namespace std;
typedef long long ll;
typedef pair<int, int> Pii;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, a, b) for (int i = (int)a; i <= (int)b; i++)
template<class T> void checkmin(T &a, T b) { if (b < a) a = b; }
template<class T> void checkmax(T &a, T b) { if (b > a) a = b; }

int N;
int A[10000];

int GCD(int a, int b) { return b == 0 ? a : GCD(b, a % b); }
int LCM(int a, int b) { return a / GCD(a, b) * b; }

void solve() {
    rep(i, N - 1) {
        int bestLcm = INT_MAX, bestVal = INT_MAX, bestIndex = -1;
        FOR(j, i + 1, N - 1) {
            int lcm = LCM(A[i], A[j]);
            if (lcm < bestLcm || lcm == bestLcm && A[j] < bestVal) {
                bestLcm = lcm;
                bestVal = A[j];
                bestIndex = j;
            }
        }
        swap(A[i + 1], A[bestIndex]);
    }
    rep(i, N) {
        if (i > 0) printf(" ");
        printf("%d", A[i]);
    }
}

int main() {
    scanf("%d", &N);
    rep(i, N) scanf("%d", &A[i]);
    solve();
    return 0;
}
0