結果

問題 No.14 最小公倍数ソート
ユーザー furafura
提出日時 2020-01-03 04:40:58
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 42 ms / 5,000 ms
コード長 865 bytes
コンパイル時間 1,699 ms
コンパイル使用メモリ 177,360 KB
実行使用メモリ 9,208 KB
最終ジャッジ日時 2023-08-14 19:04:17
合計ジャッジ時間 3,471 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,468 KB
testcase_03 AC 5 ms
4,680 KB
testcase_04 AC 42 ms
9,192 KB
testcase_05 AC 21 ms
7,036 KB
testcase_06 AC 24 ms
7,176 KB
testcase_07 AC 28 ms
7,592 KB
testcase_08 AC 33 ms
8,196 KB
testcase_09 AC 39 ms
8,828 KB
testcase_10 AC 38 ms
8,804 KB
testcase_11 AC 39 ms
8,952 KB
testcase_12 AC 40 ms
9,164 KB
testcase_13 AC 41 ms
8,936 KB
testcase_14 AC 40 ms
8,844 KB
testcase_15 AC 42 ms
9,208 KB
testcase_16 AC 23 ms
7,184 KB
testcase_17 AC 19 ms
6,636 KB
testcase_18 AC 13 ms
6,036 KB
testcase_19 AC 31 ms
8,040 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#define rep(i,n) for(int i=0;i<(n);i++)

using namespace std;

const int INF=1<<29;

vector<int> divisors(int a){
	vector<int> res;
	for(int i=1;i*i<=a;i++) if(a%i==0) {
		res.emplace_back(i);
		if(i*i<a) res.emplace_back(a/i);
	}
	sort(res.begin(),res.end());
	return res;
}

int main(){
	int n; scanf("%d",&n);
	vector<int> a(n);
	rep(i,n) scanf("%d",&a[i]);

	vector<int> D[10001];
	multiset<int> S[10001];
	rep(i,n){
		if(D[a[i]].empty()) D[a[i]]=divisors(a[i]);
		for(auto d:D[a[i]]) S[d].emplace(a[i]);
	}

	vector<int> ans(n);
	int next=a[0];
	rep(i,n){
		ans[i]=next;
		for(auto d:D[next]) S[d].erase(S[d].find(next));

		int tmp=INF;
		for(auto d:D[next]) if(!S[d].empty()) {
			tie(tmp,next)=min(make_pair(tmp,next),make_pair(*S[d].begin()/d,*S[d].begin()));
		}
	}

	rep(i,n) printf("%d%c",ans[i],i<n-1?' ':'\n');

	return 0;
}
0