結果

問題 No.14 最小公倍数ソート
ユーザー furonfuron
提出日時 2023-05-30 18:14:57
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 42 ms / 5,000 ms
コード長 2,758 bytes
コンパイル時間 1,727 ms
コンパイル使用メモリ 142,056 KB
実行使用メモリ 6,280 KB
最終ジャッジ日時 2023-08-28 00:49:40
合計ジャッジ時間 3,233 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
4,380 KB
testcase_01 AC 5 ms
4,616 KB
testcase_02 AC 5 ms
4,376 KB
testcase_03 AC 8 ms
4,420 KB
testcase_04 AC 42 ms
6,056 KB
testcase_05 AC 25 ms
5,576 KB
testcase_06 AC 27 ms
5,344 KB
testcase_07 AC 31 ms
5,624 KB
testcase_08 AC 35 ms
5,780 KB
testcase_09 AC 40 ms
5,960 KB
testcase_10 AC 39 ms
5,876 KB
testcase_11 AC 41 ms
5,968 KB
testcase_12 AC 41 ms
6,280 KB
testcase_13 AC 41 ms
6,176 KB
testcase_14 AC 40 ms
6,012 KB
testcase_15 AC 41 ms
5,944 KB
testcase_16 AC 26 ms
5,460 KB
testcase_17 AC 23 ms
5,160 KB
testcase_18 AC 16 ms
4,968 KB
testcase_19 AC 33 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <functional>
#include <cmath>
#include <string>
#include <queue>
#include <map>
#include <bitset>
#include <set>
#include <stack>
#include <numeric>
#include <unordered_map>
#include <random>

using namespace std;

using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vb = vector<bool>;
using vvb = vector<vb>;
using vd = vector<double>;
using vs = vector<string>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pdd = pair<double, double>;
using vpii = vector<pii>;
using vpll = vector<pll>;
using vpdd = vector<pdd>;
const int inf = (1 << 30) - 1;
const ll INF = 1LL << 60;
//const int MOD = 1000000007;
const int MOD = 998244353;


int main() {
	int n;
	cin >> n;
	vi a(n);
	// 並べ替えが完了していない値とインデックスの集合
	// a[0]は固定なので含めない
	set<pii> st;
	cin >> a[0];
	for (int i = 1; i < n; i++) {
		cin >> a[i];
		st.insert({ a[i], i });
	}
	int m = 10000;
	vvi div(m + 1);		// 10000までの約数のリスト

	// その数値を約数に持つ a[i]の値とそのインデックスを保存(元の値の昇順)
	// ただし、pq[1]には a[i] = 1 のみ入れる
	vector<priority_queue<pii, vector<pii>, greater<pii>>> pq(m + 1);

	for (int i = 1; i <= m; i++) {
		for (int j = i; j <= m; j += i) {
			div[j].push_back(i);
		}
	}
	for (int i = 1; i < n; i++) {
		for (int j = 0; j < div[a[i]].size(); j++) {
			pq[div[a[i]][j]].push({ a[i], i });
		}
	}

	vi ans;
	ans.push_back(a[0]);
	for (int i = 0; i < n - 1; i++) {
		int key = ans[i];
		int id = -1;
		int mi = inf; 	// 最小公倍数の最小値
		int queid = -1;

		// keyの約数全てについて、最小公倍数が最小になるものを探す
		for (int j = 0; j < div[key].size(); j++) {
			int v = div[key][j];
			if (pq[v].empty()) continue;

			pii p = pq[v].top();
			// キューの先頭の値がソート済みの時は次のキューの中身を探す
			while (st.find(p) == st.end()) {
				pq[v].pop();
				if (pq[v].empty()) break;
				p = pq[v].top();
			}
			if (pq[v].empty()) continue;

			int x = lcm(key, p.first);
			if (x < mi) {
				mi = x;
				id = p.second;
				queid = v;
			}
			else if (x == mi) {
				// 同じなら元の値が小さい方優先
				if (p.first < a[id]) {
					id = p.second;
					queid = v;
				}
			}
		}

		// pq[1]には全てのインデックスが入っているので
		// 答えは必ず見つかるはず
		ans.push_back(a[id]);
		pii p = pq[queid].top();
		pq[queid].pop();
		st.erase(p);
	}

	for (int i = 0; i < ans.size(); i++) {
		if (i > 0) cout << " ";
		cout << ans[i];
	}
	cout << endl;


	return 0;
}
0