結果

問題 No.14 最小公倍数ソート
ユーザー r_dream0r_dream0
提出日時 2017-02-09 01:19:29
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 4,693 ms / 5,000 ms
コード長 704 bytes
コンパイル時間 413 ms
コンパイル使用メモリ 59,072 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-26 17:46:12
合計ジャッジ時間 52,617 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 50 ms
4,380 KB
testcase_04 AC 4,693 ms
4,376 KB
testcase_05 AC 1,573 ms
4,376 KB
testcase_06 AC 1,841 ms
4,376 KB
testcase_07 AC 2,400 ms
4,380 KB
testcase_08 AC 3,160 ms
4,380 KB
testcase_09 AC 4,269 ms
4,376 KB
testcase_10 AC 4,197 ms
4,380 KB
testcase_11 AC 4,362 ms
4,376 KB
testcase_12 AC 4,477 ms
4,376 KB
testcase_13 AC 4,543 ms
4,376 KB
testcase_14 AC 4,413 ms
4,376 KB
testcase_15 AC 4,552 ms
4,380 KB
testcase_16 AC 1,692 ms
4,380 KB
testcase_17 AC 1,208 ms
4,376 KB
testcase_18 AC 537 ms
4,376 KB
testcase_19 AC 2,848 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <limits>
#include <cstdint>
using namespace std;

int main() {
  int32_t N;
  cin >> N;
  int32_t a[N];
  for(int32_t i = 0; i < N; i++) cin >> a[i];
  for(int32_t i = 0; i < N - 1; i++) {
    int32_t m = numeric_limits<int32_t>::max(), m2 = numeric_limits<int32_t>::max(), t = -1;
    for(int j = i + 1; j < N; j++) {
      int32_t lcm = a[i] * a[j] / __gcd(a[i], a[j]);
      if(lcm < m) {
        m = lcm;
        m2 = a[j];
        t = j;
      }else if(lcm == m && m2 > a[j]) {
        m2 = a[j];
        t = j;
      }
    }
    swap(a[i + 1], a[t]);
  }
  for(int i = 0; i < N; i++) {
    if(i) cout << " ";
    cout << a[i];
  }
  cout << endl;
}
0