結果

問題 No.14 最小公倍数ソート
ユーザー IL_mstaIL_msta
提出日時 2015-08-22 08:15:32
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2,268 ms / 5,000 ms
コード長 2,683 bytes
コンパイル時間 2,284 ms
コンパイル使用メモリ 88,364 KB
実行使用メモリ 4,388 KB
最終ジャッジ日時 2023-09-25 15:56:25
合計ジャッジ時間 29,083 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,384 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,384 KB
testcase_03 AC 71 ms
4,380 KB
testcase_04 AC 2,268 ms
4,384 KB
testcase_05 AC 760 ms
4,388 KB
testcase_06 AC 972 ms
4,380 KB
testcase_07 AC 1,268 ms
4,380 KB
testcase_08 AC 1,634 ms
4,380 KB
testcase_09 AC 2,134 ms
4,380 KB
testcase_10 AC 2,140 ms
4,380 KB
testcase_11 AC 2,138 ms
4,384 KB
testcase_12 AC 2,241 ms
4,380 KB
testcase_13 AC 2,246 ms
4,380 KB
testcase_14 AC 2,206 ms
4,380 KB
testcase_15 AC 2,227 ms
4,384 KB
testcase_16 AC 1,231 ms
4,380 KB
testcase_17 AC 972 ms
4,380 KB
testcase_18 AC 521 ms
4,380 KB
testcase_19 AC 1,713 ms
4,384 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
 
#include <iostream>
#include <iomanip>
#include <sstream>
 
#include <algorithm>
#include <cmath>
 
#include <string>
//#include <array>
#include <list>
#include <queue>
#include <vector>
#include <complex>
#include <set>
#include <map>
 
/////////
#define REP(i, x, n) for(int i = x; i < n; i++)
#define rep(i,n) REP(i,0,n)
#define P(p) cout<<(p)<<endl;
 
#define PII pair<int,int>
/////////
typedef long long LL;
typedef long double LD;
/////////
using namespace::std;
/////////
LL gcd(LL a,LL b){
	return b ? gcd(b,a%b) : a;
}
const int Amax = 10001;
int Ymax=0;
int A[Amax];

int main(void){
    std::cin.tie(0); 
    std::ios::sync_with_stdio(false);
    std::cout << std::fixed;//
    //cout << setprecision(16);//
	
	int N;
	cin>>N;
	vector<int> ans(N);
	int ansCount = 1;
	
	cin>>ans[0];
	int temp;
	for(int i=0;i<N-1;++i){
		cin>>temp;
		Ymax = max(Ymax,temp+1);
		++A[temp];
	}
	int count = 1;
	int min = 1;
	int it = 1;
	bool flag = true;
	while( ansCount < N ){
		for(count = min; count<Ymax; ++count){
			if( A[count] ){
				min = count;
				break;
			}
		}

		if( ans[ansCount-1] == 1){//?
			ans[ansCount] = min;
			--A[min];
			++ansCount;
			flag = true;
			continue;
		}
		int tempA,tempB;
		if( flag && ans[ansCount-1] < min ){
			//for(int i= ans[ansCount-1];i<Ymax;i+=i ){
			for(int i= ans[ansCount-1];flag && i<Ymax;i+= ans[ansCount-1] ){
				if( A[i] ){
					/*while(A[i]){
						ans[ansCount] = i;
						++ansCount;
						--A[i];
					}*/
					tempA = i;
					flag = false;
				}
			}
			flag = false;
		}
		LL ssk;
		int minID = min;
		{
			ssk = ans[ansCount-1]/gcd(ans[ansCount-1],minID)*minID;
			LL temp;
			for(count = min; count < ssk && count<Ymax; ++count){
				if( A[count] ){
					temp = ans[ansCount-1]/gcd(ans[ansCount-1],count)*count;
					if( temp < ssk){
						ssk = temp;
						minID = count;
					}
				}
			}
			tempB = minID;
			/*
			if( ans[ansCount-1] == minID ){
				while( A[minID] ){
					ans[ansCount] = minID;
					++ansCount;
					--A[minID];
				}
			}else{
				ans[ansCount] = minID;
				++ansCount;
				--A[minID];
			}*/
			flag = true;
		}
		if( ssk < ans[ansCount-1]/gcd(ans[ansCount-1],tempB)*tempB ){
			if( ans[ansCount-1] == minID ){
				while( A[minID] ){
					ans[ansCount] = minID;
					++ansCount;
					--A[minID];
				}
			}else{
				ans[ansCount] = minID;
				++ansCount;
				--A[minID];
			}
		}else{//tempA
			while(A[tempB]){
				ans[ansCount] = tempB;
				++ansCount;
				--A[tempB];
			}
		}
	}
	
	vector<int>::iterator titr;
	titr = ans.begin();
	while( titr != ans.end() ){
		cout << *titr;
		if( titr+1 != ans.end() ){
			cout << ' ';
		}
		++titr;
	}cout << '\n';
	return 0;
}
0