結果

問題 No.14 最小公倍数ソート
ユーザー IL_mstaIL_msta
提出日時 2015-08-23 04:24:02
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 13 ms / 5,000 ms
コード長 3,345 bytes
コンパイル時間 731 ms
コンパイル使用メモリ 90,852 KB
実行使用メモリ 4,452 KB
最終ジャッジ日時 2023-09-25 16:15:56
合計ジャッジ時間 1,922 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,384 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 7 ms
4,376 KB
testcase_04 AC 13 ms
4,452 KB
testcase_05 AC 7 ms
4,376 KB
testcase_06 AC 8 ms
4,376 KB
testcase_07 AC 9 ms
4,376 KB
testcase_08 AC 11 ms
4,376 KB
testcase_09 AC 13 ms
4,380 KB
testcase_10 AC 13 ms
4,380 KB
testcase_11 AC 13 ms
4,384 KB
testcase_12 AC 13 ms
4,384 KB
testcase_13 AC 12 ms
4,380 KB
testcase_14 AC 12 ms
4,376 KB
testcase_15 AC 12 ms
4,380 KB
testcase_16 AC 11 ms
4,380 KB
testcase_17 AC 10 ms
4,380 KB
testcase_18 AC 10 ms
4,380 KB
testcase_19 AC 11 ms
4,376 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;
/////////

const int Amax = 10001;
int A[Amax];
bool pdp[Amax];
vector<vector<int>> dp(Amax);
vector<int> ans(Amax);

void makePrime(int N){
	int i,j;
	for( i=2; i<= N; ++i){
		if(pdp[i] == false){
			for( j = i+i; j<= N; j += i){
				pdp[j] = true;
				dp[j].push_back(i);
			}
		}else{
			for( j = i+i; j<= N; j += i){
				dp[j].push_back(i);
			}
		}
		dp[i].push_back(i);
	}
	return ;
}

int main(void){
    std::cin.tie(0); 
    std::ios::sync_with_stdio(false);
    std::cout << std::fixed;//
    //cout << setprecision(16);//
	
	int N;
	cin>>N;
	
	int Ymax=0;
	int ansCount = 1;
	
	cin>>ans[0];
	int cintemp;
	for(int i=0;i<N-1;++i){
		cin>>cintemp;
		Ymax = max(Ymax,cintemp+1);
		++A[cintemp];
	}
	makePrime(Ymax-1);
	int count = 1;
	int minNum = 1;//残った列での最小値
	
	while( A[1] ){//1を全部使う
		ans[ansCount] = 1;
		++ansCount;
		--A[1];
	}
	for(count = minNum; count<Ymax; ++count){
		if( A[count] ){
			minNum = count;
			break;
		}
	}
	if( ans[ansCount-1] == 1){
		while( A[minNum] ){
			ans[ansCount] = minNum;
			--A[minNum];
			++ansCount;
		}
	}

	int ter;//固定された最後の値
	bool ansFlag;
	int i;//ループカウンタ
	int ansA,ansB;//素数の倍数 と 最小値
	int temp;//一時収納
	vector<int>::iterator yItr;
	vector<int>::iterator yBegin;
	vector<int>::iterator yEnd;

	while( ansCount < N ){
		for(count = minNum; count<Ymax; ++count){
			if( A[count] ){
				minNum = count;
				break;
			}
		}

		ter = ans[ansCount-1];
		ansFlag = false;

		if( pdp[ter] == false ){//素数
			ansB = minNum*ter;
			temp = min( Ymax, ansB);
			for( i=minNum/ter; i*ter < temp ; ++i){
				if( A[i*ter] ){
					ansA = i*ter;
					ansFlag = true;
					break;
				}
			}
			
			if( ansFlag ){
				if( ansB < ansA ){
					ans[ansCount] = minNum;
					--A[minNum];
					++ansCount;
				}else{//ansA
					ans[ansCount] = ansA;
					--A[ansA];
					++ansCount;
				}
			}else{
				ans[ansCount] = minNum;
				--A[minNum];
				++ansCount;
			}
			continue;
		}
		
		//////////
		yItr	= dp[ ter ].begin();
		yBegin	= yItr;
		yEnd	= dp[ ter ].end();
		
		i= minNum/ter;
		if( minNum%ter ) ++i;
		if( yItr != yEnd )
		{
			for( ; !ansFlag && i <= minNum ; ++i ){
				{
					temp = i ;//約数1
					if( temp < Ymax && A[temp] ){
						ans[ansCount] = temp;
						--A[temp];
						++ansCount;
						ansFlag = true;
						break;
					}
				}
				if(!ansFlag){
					while( yItr != yEnd){
						temp = i * (*yItr);
						if( temp < Ymax && A[temp] ){
							ans[ansCount] = temp;
							--A[temp];
							++ansCount;
							ansFlag = true;
							break;
						}
						++yItr;
					}
				}
				
				yItr = yBegin;
			}
		}

		if( !ansFlag ){
			ans[ansCount] = minNum;
			--A[minNum];
			++ansCount;
		}
	}
	////////////
	cout << ans[0];
	for(i=1;i<N;++i){
		cout << ' ' << ans[i];
	}cout << '\n';
	return 0;
}
0