結果

問題 No.14 最小公倍数ソート
ユーザー heno239heno239
提出日時 2018-11-02 23:53:13
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 27 ms / 5,000 ms
コード長 1,945 bytes
コンパイル時間 1,389 ms
コンパイル使用メモリ 124,244 KB
実行使用メモリ 13,056 KB
最終ジャッジ日時 2024-04-30 18:18:07
合計ジャッジ時間 2,917 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 11 ms
11,392 KB
testcase_01 AC 11 ms
11,392 KB
testcase_02 AC 11 ms
11,392 KB
testcase_03 AC 13 ms
11,520 KB
testcase_04 AC 25 ms
12,928 KB
testcase_05 AC 19 ms
12,288 KB
testcase_06 AC 21 ms
12,288 KB
testcase_07 AC 21 ms
12,288 KB
testcase_08 AC 24 ms
12,672 KB
testcase_09 AC 25 ms
12,928 KB
testcase_10 AC 25 ms
12,928 KB
testcase_11 AC 25 ms
12,928 KB
testcase_12 AC 26 ms
12,928 KB
testcase_13 AC 26 ms
12,928 KB
testcase_14 AC 26 ms
13,056 KB
testcase_15 AC 27 ms
12,928 KB
testcase_16 AC 20 ms
12,288 KB
testcase_17 AC 19 ms
12,160 KB
testcase_18 AC 16 ms
11,904 KB
testcase_19 AC 23 ms
12,544 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
using namespace std;
typedef long long ll;
typedef unsigned int ui;
const ll mod = (ll)(1e+9) + 7;
const ll INF = (ll)1000000007 * 1000000007;
typedef pair<int, int> P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef long double ld;
typedef complex<ld> Point;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<ll, ll> LP;
typedef pair<ld, ld> LDP;
const int key = 10000;
vector<int> d[key+1];
vector<P> v[key+1];
queue<P> q[key + 1];
int n, a[key+1];
void init() {
	rep1(i, key) {
		for (int j = i; j <= key; j += i) {
			d[j].push_back(i);
		}
	}
}
bool used[key + 1];
vector<int> ans;
int main(){
	init();
	cin >> n;
	rep(i, n) {
		cin >> a[i];
		rep(j, (int)d[a[i]].size()) {
			int t = d[a[i]][j];
			v[t].push_back({ a[i]/t,i });
		}
	}
	rep1(i, key) {
		sort(v[i].begin(), v[i].end());
		rep(j, (int)v[i].size()) {
			q[i].push(v[i][j]);
		}
	}
	used[0] = true;
	int m = a[0];
	ans.push_back(a[0]);
	rep(i, n - 1) {
		int ma = 2 * key; int nextid = -1;
		rep(j, d[m].size()) {
			int dd = d[m][j];
			while (!q[dd].empty() && used[q[dd].front().second])q[dd].pop();
			if (q[dd].empty())continue;
			if (ma > q[dd].front().first) {
				ma = q[dd].front().first;
				nextid = q[dd].front().second;
			}
		}
		ans.push_back(a[nextid]);
		m = a[nextid];
		used[nextid] = true;
	}
	rep(i, (int)ans.size()) {
		if (i > 0) {
			cout << " ";
		}
		cout << ans[i];
	}
	cout << endl;
	return 0;
}
0