結果

問題 No.14 最小公倍数ソート
ユーザー masamasa
提出日時 2015-01-22 04:57:00
言語 C++11
(gcc 11.4.0)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 721 bytes
コンパイル時間 1,299 ms
コンパイル使用メモリ 62,476 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-05 03:23:11
合計ジャッジ時間 59,012 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 54 ms
4,380 KB
testcase_04 TLE -
testcase_05 AC 1,770 ms
4,380 KB
testcase_06 AC 2,064 ms
4,380 KB
testcase_07 AC 2,680 ms
4,376 KB
testcase_08 AC 3,532 ms
4,380 KB
testcase_09 AC 4,786 ms
4,384 KB
testcase_10 AC 4,663 ms
4,380 KB
testcase_11 AC 4,822 ms
4,380 KB
testcase_12 AC 4,958 ms
4,384 KB
testcase_13 TLE -
testcase_14 AC 4,874 ms
4,384 KB
testcase_15 TLE -
testcase_16 AC 1,867 ms
4,376 KB
testcase_17 AC 1,337 ms
4,376 KB
testcase_18 AC 596 ms
4,376 KB
testcase_19 AC 3,162 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <utility>

using namespace std;

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

long lcm(int a, int b) {
	return (long) a * b / gcd(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 pos = i + 1;
		long mini = 1e18;
		for (int j = i + 1; j < n; j++) {
			long tmp = lcm(a[i], a[j]);
			if (tmp < mini || (tmp == mini && a[j] < a[pos]) ) {
				mini = tmp;
				pos = j;
			}
		}
		swap(a[i+1], a[pos]);
	}

	for (int i = 0; i < n; i++) {
		printf("%d%c", a[i], i != n - 1 ? ' ' : '\n');
	}
	return 0;
}
0