結果

問題 No.14 最小公倍数ソート
ユーザー furafura
提出日時 2020-01-03 04:40:58
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 55 ms / 5,000 ms
コード長 865 bytes
コンパイル時間 1,920 ms
コンパイル使用メモリ 179,128 KB
実行使用メモリ 9,216 KB
最終ジャッジ日時 2024-05-02 07:04:32
合計ジャッジ時間 3,695 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 5 ms
5,376 KB
testcase_04 AC 47 ms
9,088 KB
testcase_05 AC 26 ms
7,040 KB
testcase_06 AC 29 ms
7,296 KB
testcase_07 AC 32 ms
7,936 KB
testcase_08 AC 39 ms
8,320 KB
testcase_09 AC 47 ms
8,960 KB
testcase_10 AC 49 ms
8,832 KB
testcase_11 AC 53 ms
8,960 KB
testcase_12 AC 50 ms
9,088 KB
testcase_13 AC 55 ms
9,088 KB
testcase_14 AC 53 ms
9,088 KB
testcase_15 AC 53 ms
9,216 KB
testcase_16 AC 28 ms
7,424 KB
testcase_17 AC 23 ms
6,912 KB
testcase_18 AC 15 ms
6,272 KB
testcase_19 AC 38 ms
8,192 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