結果

問題 No.14 最小公倍数ソート
コンテスト
ユーザー k
提出日時 2014-10-31 14:14:21
言語 C++11(old_compat)
(gcc 12.4.0 + boost 1.89.0)
コンパイル:
g++-12 -O2 -lm -std=gnu++11 -Wuninitialized -DONLINE_JUDGE -include bits/stdc++.h -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 1,342 ms / 5,000 ms
コード長 1,251 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 3,178 ms
コンパイル使用メモリ 173,552 KB
実行使用メモリ 7,848 KB
最終ジャッジ日時 2026-03-08 15:58:53
合計ジャッジ時間 19,792 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 20
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:32:41: warning: format ‘%llu’ expects argument of type ‘long long unsigned int*’, but argument 2 has type ‘__gnu_cxx::__alloc_traits<std::allocator<long unsigned int>, long unsigned int>::value_type*’ {aka ‘long unsigned int*’} [-Wformat=]
   32 |   for (int i = 0; i < n; i++) scanf("%llu", &a[i]);
      |                                      ~~~^
      |                                         |
      |                                         long long unsigned int*
      |                                      %lu
main.cpp:35:16: warning: format ‘%llu’ expects argument of type ‘long long unsigned int’, but argument 2 has type ‘__gnu_cxx::__alloc_traits<std::allocator<long unsigned int>, long unsigned int>::value_type’ {aka ‘long unsigned int’} [-Wformat=]
   35 |    printf ("%llu ", a[0]);
      |             ~~~^
      |                |
      |                long long unsigned int
      |             %lu
main.cpp:47:20: warning: format ‘%llu’ expects argument of type ‘long long unsigned int’, but argument 2 has type ‘__gnu_cxx::__alloc_traits<std::allocator<long unsigned int>, long unsigned int>::value_type’ {aka ‘long unsigned int’} [-Wformat=]
   47 |        printf ("%llu ", a[p]);
      |                 ~~~^
      |                    |
      |                    long long unsigned int
      |                 %lu
main.cpp:32:36: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   32 |   for (int i = 0; i < n; i++) scanf("%llu", &a[i]);
      |                               ~~~~~^~~~~~~~~~~~~~~

ソースコード

diff #
raw source code

#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