結果

問題 No.14 最小公倍数ソート
ユーザー kk
提出日時 2014-10-31 14:14:21
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 3,562 ms / 5,000 ms
コード長 1,251 bytes
コンパイル時間 976 ms
コンパイル使用メモリ 78,488 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-29 00:00:01
合計ジャッジ時間 42,000 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 55 ms
4,376 KB
testcase_04 AC 3,562 ms
4,376 KB
testcase_05 AC 1,184 ms
4,376 KB
testcase_06 AC 1,438 ms
4,380 KB
testcase_07 AC 1,865 ms
4,376 KB
testcase_08 AC 2,457 ms
4,376 KB
testcase_09 AC 3,282 ms
4,376 KB
testcase_10 AC 3,273 ms
4,376 KB
testcase_11 AC 3,311 ms
4,376 KB
testcase_12 AC 3,460 ms
4,380 KB
testcase_13 AC 3,496 ms
4,376 KB
testcase_14 AC 3,376 ms
4,380 KB
testcase_15 AC 3,506 ms
4,376 KB
testcase_16 AC 1,447 ms
4,384 KB
testcase_17 AC 1,073 ms
4,376 KB
testcase_18 AC 512 ms
4,376 KB
testcase_19 AC 2,324 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <algorithm>
#include <functional>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cctype>
#include <string>

using namespace std;

typedef uint64_t ll;

ll gcd(ll a, ll b){
   if(b==0)return a;
   else return gcd(b, a%b);
}

ll lcm(ll a, ll b){
   return a / gcd(a,b) * b;
}

int main() {
  ll n;
  cin >> n;
  vector <ll> a(n);
  for (int i = 0; i < n; i++) scanf("%llu", &a[i]);
  sort(a.begin()+1, a.end());

   printf ("%llu ", a[0]);
  for (int i = 0; i < n-1; i++) {
    ll mini = 1000000000;
    int p = -1;

    for (int j = i+1; j < n; j++) {
      if (p != -1 && mini < a[j]) break;
      ll t = lcm(a[i], a[j]);
      if (mini > t) p = j;
      if (p != -1 && mini == t && a[p] > a[j]) p = j;
      mini = min(mini, t);
    }
       printf ("%llu ", a[p]);
    if (i == n-2) break;
    if (i+1 == p) continue;
    swap(a[p], a[i+1]);
    // for (int j = 0; j < a.size(); j++) cout << a[j] << " ";
    // cout << endl;

    ll x = a[p]; a.erase(a.begin()+p);
    a.insert(lower_bound(a.begin()+i+2, a.end(), x), x);

    //    sort(a+i+2, a+n);
  }
}
0