結果
問題 | No.14 最小公倍数ソート |
ユーザー |
![]() |
提出日時 | 2015-03-18 14:01:43 |
言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
結果 |
AC
|
実行時間 | 4,422 ms / 5,000 ms |
コード長 | 798 bytes |
コンパイル時間 | 533 ms |
コンパイル使用メモリ | 65,208 KB |
実行使用メモリ | 6,944 KB |
最終ジャッジ日時 | 2024-06-28 23:14:42 |
合計ジャッジ時間 | 49,238 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 20 |
ソースコード
#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; }