結果

問題 No.14 最小公倍数ソート
ユーザー heno239heno239
提出日時 2018-10-31 11:19:31
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,624 bytes
コンパイル時間 1,041 ms
コンパイル使用メモリ 112,692 KB
実行使用メモリ 8,960 KB
最終ジャッジ日時 2024-04-30 01:19:47
合計ジャッジ時間 7,892 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
using namespace std;
typedef long long ll;
typedef unsigned int ui;
const ll mod = (ll)(1e+9) + 7;
const ll INF = (ll)1000000007 * 1000000007;
typedef pair<int, int> P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef long double ld;
typedef complex<ld> Point;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<ll, ll> LP;
typedef pair<ld, ld> LDP;
ll a[1 << 14];
bool used[1 << 14];
ll gcd(ll x, ll y) {
	if (x < y)swap(x, y);
	while (y) {
		ll r = x % y; x = y; y = r;
	}
	return x;
}
ll lcm(ll x, ll y) {
	return x * y / gcd(x, y);
}
int main(){
	int n; cin >> n;
	rep(i, n) {
		cin >> a[i];
	}
	used[0] = true;
	vector<ll> ans;
	ans.push_back(a[0]);
	ll memo = a[0];
	rep(aa, n - 1) {
		int chk = -1;
		ll mi = INF; ll mi2 = mod;
		rep(i, n) {
			if (used[i])continue;
			ll l = lcm(memo, a[i]);
			if (l < mi) {
				mi = l; mi2 = a[i]; chk = i;
			}
			else if (l == mi&&a[i]<mi2) {
				mi2 = a[i]; chk = i;
			}
		}
		ans.push_back(a[chk]);
		used[chk] = true;
		memo = a[chk];
	}
	rep(i, n) {
		if (i > 0)cout << " ";
		cout << ans[i];
	}
	cout << endl;
	return 0;
}
0