結果

問題 No.14 最小公倍数ソート
ユーザー hotpepsihotpepsi
提出日時 2015-03-18 14:01:43
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 4,992 ms / 5,000 ms
コード長 798 bytes
コンパイル時間 498 ms
コンパイル使用メモリ 66,572 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-11 08:36:43
合計ジャッジ時間 55,740 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 53 ms
4,380 KB
testcase_04 AC 4,992 ms
4,380 KB
testcase_05 AC 1,676 ms
4,384 KB
testcase_06 AC 1,955 ms
4,380 KB
testcase_07 AC 2,552 ms
4,376 KB
testcase_08 AC 3,355 ms
4,376 KB
testcase_09 AC 4,541 ms
4,384 KB
testcase_10 AC 4,461 ms
4,376 KB
testcase_11 AC 4,626 ms
4,376 KB
testcase_12 AC 4,770 ms
4,384 KB
testcase_13 AC 4,827 ms
4,380 KB
testcase_14 AC 4,688 ms
4,384 KB
testcase_15 AC 4,839 ms
4,380 KB
testcase_16 AC 1,797 ms
4,380 KB
testcase_17 AC 1,285 ms
4,376 KB
testcase_18 AC 570 ms
4,380 KB
testcase_19 AC 3,035 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <sstream>
#include <vector>
#include <cstring>

using namespace std;

int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
int lcm(int a, int b) { return (a / gcd(a, b)) * b; }

int main(int argc, char *argv[])
{
	string s;
	getline(cin, s);
	int N = atoi(s.c_str());

	vector<int> A(N), U(N);
	getline(cin, s);
	stringstream ss(s);
	for (int i = 0; i < N; ++i) {
		ss >> A[i];
	}

	int selected = 0;
	for (int i = 0; i < N; ++i) {
		int b = A[selected];
		if (i > 0) {
			cout << " ";
		}
		cout << b;
		U[selected] = 1;

		int x = 1 << 30;
		for (int j = 0; j < N; ++j) {
			if (!U[j]) {
				int c = lcm(b, A[j]);
				if (c < x || (c == x && A[j] < A[selected])) {
					x = c;
					selected = j;
				}
			}
		}
	}
	cout << endl;

	return 0;
}
0