結果

問題 No.14 最小公倍数ソート
ユーザー masamasa
提出日時 2015-01-22 05:28:16
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 4,631 ms / 5,000 ms
コード長 615 bytes
コンパイル時間 1,319 ms
コンパイル使用メモリ 63,188 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-05 03:32:40
合計ジャッジ時間 52,380 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 48 ms
4,380 KB
testcase_04 AC 4,631 ms
4,380 KB
testcase_05 AC 1,555 ms
4,384 KB
testcase_06 AC 1,820 ms
4,380 KB
testcase_07 AC 2,377 ms
4,380 KB
testcase_08 AC 3,123 ms
4,380 KB
testcase_09 AC 4,244 ms
4,384 KB
testcase_10 AC 4,134 ms
4,384 KB
testcase_11 AC 4,293 ms
4,380 KB
testcase_12 AC 4,419 ms
4,380 KB
testcase_13 AC 4,488 ms
4,384 KB
testcase_14 AC 4,343 ms
4,380 KB
testcase_15 AC 4,491 ms
4,384 KB
testcase_16 AC 1,666 ms
4,380 KB
testcase_17 AC 1,190 ms
4,384 KB
testcase_18 AC 527 ms
4,384 KB
testcase_19 AC 2,829 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

using namespace std;

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;
		int mini = INT_MAX;
		for (int j = i + 1; j < n; j++) {
			int tmp = a[i] / __gcd(a[i], a[j]) * 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