結果

問題 No.14 最小公倍数ソート
ユーザー NotationNapNotationNap
提出日時 2015-04-23 07:44:43
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 4,659 ms / 5,000 ms
コード長 641 bytes
コンパイル時間 1,091 ms
コンパイル使用メモリ 144,792 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-18 08:33:53
合計ジャッジ時間 52,565 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 49 ms
4,376 KB
testcase_04 AC 4,659 ms
4,380 KB
testcase_05 AC 1,560 ms
4,380 KB
testcase_06 AC 1,821 ms
4,380 KB
testcase_07 AC 2,379 ms
4,376 KB
testcase_08 AC 3,131 ms
4,376 KB
testcase_09 AC 4,248 ms
4,376 KB
testcase_10 AC 4,161 ms
4,380 KB
testcase_11 AC 4,311 ms
4,376 KB
testcase_12 AC 4,441 ms
4,380 KB
testcase_13 AC 4,490 ms
4,376 KB
testcase_14 AC 4,368 ms
4,376 KB
testcase_15 AC 4,507 ms
4,376 KB
testcase_16 AC 1,680 ms
4,376 KB
testcase_17 AC 1,196 ms
4,376 KB
testcase_18 AC 534 ms
4,380 KB
testcase_19 AC 2,827 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;

typedef long long Int;
#define REP(i,n) for(int (i)=0;(i)<(int)(n);++(i))

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;
}

int a[10000];
int main() {
	int N;
	cin >> N;
	REP(i, N) cin >> a[i];
	
	REP(i, N-1) {
		int k = i + 1;
		int m = lcm(a[i], a[i + 1]);
		for (int j = i + 2; j < N; j++) {
			int m1 = lcm(a[i], a[j]);
			if (m > m1 || m == m1 && a[k] > a[j]) {
				m = m1;
				k = j;
			}
		}
		if (i + 1 != k)
			swap(a[i + 1], a[k]);
	}
	cout << a[0];
	for (int i = 1; i < N; i++) cout << " " << a[i];
	cout << endl;

}
0