結果

問題 No.14 最小公倍数ソート
ユーザー pekempeypekempey
提出日時 2015-08-21 17:01:41
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 960 bytes
コンパイル時間 1,162 ms
コンパイル使用メモリ 146,724 KB
実行使用メモリ 4,504 KB
最終ジャッジ日時 2023-09-25 14:23:58
合計ジャッジ時間 2,583 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i, a) rep2 (i, 0, a)
#define rep2(i, a, b) for (int i = (a); i < (b); i++)
#define repr(i, a) repr2 (i, 0, a)
#define repr2(i, a, b) for (int i = (b) - 1; i >= (a); i--)
#define asn(a, b, c) fill_n(&(b), sizeof(a) / sizeof(b), c)
using namespace std;
typedef long long ll;
const ll inf = 1e9;
const ll mod = 1e9 + 7;
const ll infl = 1e18;

ll lcm(ll a, ll b) {
	return a / __gcd(a, b) * b;
}

ostream &operator <<(ostream &os, const vector<ll> &v) {
	rep (i, v.size()) {
		if (i) cout << " ";
		cout << v[i];
	}
	return os;
}

int main() {
	int N;
	cin >> N;
	vector<ll> a(N);
	rep (i, N) cin >> a[i];

	rep (i, N - 1) {
		ll mini = infl;
		ll minv = infl;
		rep2 (j, i + 1, N) {
			if (a[i] * a[j] > minv) continue;
			ll l = lcm(a[i], a[j]);
			if (l < minv) {
				minv = l;
				mini = j;
			} else if (l == minv && a[mini] > a[j]) {
				mini = j;
			}
		}
		swap(a[i + 1], a[mini]);
	}

	cout << a << endl;

	return 0;
}
0