結果

問題 No.14 最小公倍数ソート
ユーザー IL_mstaIL_msta
提出日時 2015-08-22 07:01:41
言語 C++11
(gcc 11.4.0)
結果
TLE  
実行時間 -
コード長 1,956 bytes
コンパイル時間 1,618 ms
コンパイル使用メモリ 93,216 KB
実行使用メモリ 7,900 KB
最終ジャッジ日時 2023-09-25 15:53:23
合計ジャッジ時間 8,913 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,384 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,384 KB
testcase_03 AC 425 ms
4,376 KB
testcase_04 TLE -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
権限があれば一括ダウンロードができます

ソースコード

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;
}

int A[10000];

const int NumMax = 10000;
bool pdp[NumMax+1];
vector<int> prime;
void makePrime(int N=NumMax){
	for(int i=2; i<= N; ++i){
		if(pdp[i] == false){
			prime.push_back(i);
			for(int j = i+i; j<= N; j += i){
				pdp[j] = true;
			}
		}
	}
	return ;
}

int main(void){
    std::cin.tie(0); 
    std::ios::sync_with_stdio(false);
    std::cout << std::fixed;//
    //cout << setprecision(16);//
	
	const int Amax = 10001;
	makePrime();
	int N;
	cin>>N;
	vector<int> ans(N);
	int ansCount = 1;

	vector<int> vec(N-1);
	list<int> myList;
	cin>>ans[0];
	for(int i=0;i<N-1;++i){
		cin>>vec[i];
	}
	sort( vec.begin(), vec.end() );
	vector<int>::iterator titr = vec.begin();
	while( titr != vec.end() ){
		myList.push_back(*titr);
		++titr;
	}
	myList.push_back(Amax);
	
	LL ssk,tssk;
	list<int>::iterator itr,minIt;
	while( ansCount < N ){
		itr = myList.begin();
		minIt = itr;
		ssk = ans[ansCount-1]/gcd(ans[ansCount-1],*itr)*(*itr);
		++itr;

		while( *itr != Amax && (*itr) < ssk ){
			tssk = ans[ansCount-1]/gcd(ans[ansCount-1],*itr)*(*itr);
			if( tssk < ssk ){
				minIt = itr;
				ssk = tssk;
			}
			//++itr;
			itr = upper_bound(itr,myList.end(),(*itr));
		}

		ans[ansCount] = *minIt;
		myList.erase(minIt);
		++ansCount;
	}
	
	titr = ans.begin();
	while( titr != ans.end() ){
		cout << *titr;
		if( titr+1 != ans.end() ){
			cout << ' ';
		}
		++titr;
	}cout << '\n';
	return 0;
}
0