結果

問題 No.14 最小公倍数ソート
ユーザー Yang33Yang33
提出日時 2018-04-09 00:52:41
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 55 ms / 5,000 ms
コード長 2,931 bytes
コンパイル時間 1,927 ms
コンパイル使用メモリ 178,520 KB
実行使用メモリ 8,916 KB
最終ジャッジ日時 2023-09-09 03:18:50
合計ジャッジ時間 3,510 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 5 ms
4,376 KB
testcase_04 AC 55 ms
8,856 KB
testcase_05 AC 28 ms
6,544 KB
testcase_06 AC 30 ms
6,816 KB
testcase_07 AC 37 ms
7,212 KB
testcase_08 AC 43 ms
7,868 KB
testcase_09 AC 51 ms
8,536 KB
testcase_10 AC 51 ms
8,260 KB
testcase_11 AC 53 ms
8,664 KB
testcase_12 AC 52 ms
8,672 KB
testcase_13 AC 54 ms
8,712 KB
testcase_14 AC 52 ms
8,716 KB
testcase_15 AC 54 ms
8,916 KB
testcase_16 AC 30 ms
6,808 KB
testcase_17 AC 25 ms
6,156 KB
testcase_18 AC 16 ms
5,624 KB
testcase_19 AC 40 ms
8,004 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

using VS = vector<string>;    using LL = long long;
using VI = vector<int>;       using VVI = vector<VI>;
using PII = pair<int, int>;   using PLL = pair<LL, LL>;
using VL = vector<LL>;        using VVL = vector<VL>;

#define ALL(a)  begin((a)),end((a))
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define SZ(a) int((a).size())
#define SORT(c) sort(ALL((c)))
#define RSORT(c) sort(RALL((c)))
#define UNIQ(c) (c).erase(unique(ALL((c))), end((c)))
#define FOR(i, s, e) for (int(i) = (s); (i) < (e); (i)++)
#define FORR(i, s, e) for (int(i) = (s); (i) > (e); (i)--)
#define debug(x) cerr << #x << ": " << x << endl
const int INF = 1e9;                          const LL LINF = 1e16;
const LL MOD = 1000000007;                    const double PI = acos(-1.0);
int DX[8] = { 0, 0, 1, -1, 1, 1, -1, -1 };    int DY[8] = { 1, -1, 0, 0, 1, -1, 1, -1 };

/* -----  2018/04/08  Problem: yukicoder 014  / Link: http://yukicoder.me/problems/no/014  ----- */
/* ------問題------

最小公倍数を習ったばかりのLarryは、最小公倍数ソートというのを思いついた。
ここで「最小公倍数ソート」とは、
N個の整数(重複を含む)が与えられ、それぞれai (1≤i≤N)とする。
a1を固定し、a2〜aNをそれぞれa1に対して最小公倍数を取り、最小公倍数が小さい順に並べ変えるソートのことであるとする。
(最小公倍数の最小が複数ある場合は、元の数が少ない方が優先される)

Larryは、
この時出来た配列を新たにa1…aNの名前をつけなおして操作を続ける。
次にa2を固定し(a1も固定したまま)、a3〜aNを「最小公倍数ソート」していく。
次にa3を固定し...と続けていった時にaNまで行った時になる数列を求めてください。

-----問題ここまで----- */
/* -----解説等-----

男は黙って非naive解法!
本番ならnaive解法!

----解説ここまで---- */

LL N;

int main() {
	cin.tie(0);
	ios_base::sync_with_stdio(false);

	cin >> N;
	VI a(N);
	VVI divs(N);

	vector<set<PII>> S(10001);
	FOR(i, 0, N) {
		cin >> a[i];
		for (int j = 1; j*j <= a[i]; j++) {
			if (a[i] % j == 0) {
				divs[i].push_back(j);
				S[j].insert(PII(a[i], i));
				if (j*j != a[i]) {
					divs[i].push_back(a[i] / j);
					S[a[i] / j].insert(PII(a[i], i));
				}
			}
		}
	}

	int id = 0;
	FOR(_, 0, N) {
		cout << a[id] << (_ == N - 1 ? "\n" : " ");

		PII Min = PII(INF, INF);
		int tempid = -1;
		FOR(i, 0, SZ(divs[id])) {
			S[divs[id][i]].erase(PII(a[id], id));//使っちゃいけないので
			if (S[divs[id][i]].empty())continue;
			PII b = *S[divs[id][i]].begin();
			PII comp = PII(a[id] / divs[id][i] * a[b.second], b.first);
			if (Min > comp) {
				Min = comp;
				tempid = b.second;
			}
		}
		id = tempid;
	}

	return 0;
}
0