結果

問題 No.14 最小公倍数ソート
ユーザー IL_mstaIL_msta
提出日時 2015-08-22 03:01:15
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 4,193 ms / 5,000 ms
コード長 1,626 bytes
コンパイル時間 853 ms
コンパイル使用メモリ 92,140 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-25 15:51:50
合計ジャッジ時間 48,746 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,384 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 64 ms
4,376 KB
testcase_04 AC 4,193 ms
4,376 KB
testcase_05 AC 1,394 ms
4,380 KB
testcase_06 AC 1,689 ms
4,380 KB
testcase_07 AC 2,191 ms
4,380 KB
testcase_08 AC 2,895 ms
4,380 KB
testcase_09 AC 3,857 ms
4,380 KB
testcase_10 AC 3,844 ms
4,376 KB
testcase_11 AC 3,898 ms
4,376 KB
testcase_12 AC 4,076 ms
4,376 KB
testcase_13 AC 4,121 ms
4,376 KB
testcase_14 AC 3,968 ms
4,376 KB
testcase_15 AC 4,125 ms
4,380 KB
testcase_16 AC 1,694 ms
4,376 KB
testcase_17 AC 1,253 ms
4,376 KB
testcase_18 AC 597 ms
4,376 KB
testcase_19 AC 2,723 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;
}

int A[10000];

int main(void){
    std::cin.tie(0); 
    std::ios::sync_with_stdio(false);
    std::cout << std::fixed;//
    //cout << setprecision(16);//
	
	const int Amax = 10001;
	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;
	}

	
	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 != myList.end() && (*itr) < ssk ){
			tssk = ans[ansCount-1]/gcd(ans[ansCount-1],*itr)*(*itr);
			if( tssk < ssk ){
				minIt = itr;
				ssk = tssk;
			}
			++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