結果

問題 No.14 最小公倍数ソート
ユーザー kyuridenamidakyuridenamida
提出日時 2016-02-10 21:10:05
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 26 ms / 5,000 ms
コード長 1,089 bytes
コンパイル時間 2,502 ms
コンパイル使用メモリ 168,384 KB
実行使用メモリ 5,184 KB
最終ジャッジ日時 2023-10-21 22:28:44
合計ジャッジ時間 2,636 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 4 ms
4,348 KB
testcase_04 AC 26 ms
5,184 KB
testcase_05 AC 15 ms
4,668 KB
testcase_06 AC 16 ms
4,736 KB
testcase_07 AC 19 ms
4,848 KB
testcase_08 AC 22 ms
4,968 KB
testcase_09 AC 25 ms
5,048 KB
testcase_10 AC 24 ms
5,044 KB
testcase_11 AC 25 ms
5,144 KB
testcase_12 AC 26 ms
5,152 KB
testcase_13 AC 26 ms
5,104 KB
testcase_14 AC 25 ms
5,076 KB
testcase_15 AC 26 ms
5,080 KB
testcase_16 AC 16 ms
4,756 KB
testcase_17 AC 14 ms
4,632 KB
testcase_18 AC 10 ms
4,432 KB
testcase_19 AC 21 ms
4,932 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;


typedef pair<int,int> P;

int a[10010];
int res[10010];
int done[10010];
priority_queue<P,vector<P>,greater<P>> Q[10010];
int main(){
	int N;
	cin >> N;
	
	for(int i = 0 ; i < N ; i++){
		cin >> a[i];
		for(int j = 1 ; j * j <= a[i] ; j++){
			if( a[i] % j == 0 ){
				Q[a[i]/j].push({a[i],i});
				Q[j].push({a[i],i});
			}
		}
	}
	int p;
	for(int i = 0 ; i < N ; i++){
		if( i == 0 ){
			res[0] = 0;
			p = a[0];
			done[0] = 1;
		}else{
			vector<int> d;
			for(int j = 1 ; j * j <= p ; j++){
				if( p % j == 0 ){
					d.push_back(p/j);
					d.push_back(j);
				}
			}
			array<int,3> cand = {INT_MAX,INT_MAX,INT_MAX};
			for( auto e : d ){
				while( Q[e].size() && done[Q[e].top().second] ) Q[e].pop();
				if( Q[e].size() ){
					cand = min(cand,{p / e *  Q[e].top().first,Q[e].top().first,Q[e].top().second});
				}
			}
			p = cand[1];
			res[i] = cand[2];
			// cout << cand.first << " " << cand.second << "|" << endl;
			done[cand[2]] = 1;
			
		}
	}
	
	for(int i = 0 ; i < N ; i++){
		cout << a[res[i]] << (i+1==N?"\n":" ");
	}
}
0