結果

問題 No.14 最小公倍数ソート
ユーザー どららどらら
提出日時 2015-06-02 21:56:25
言語 C++11
(gcc 11.4.0)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,525 bytes
コンパイル時間 727 ms
コンパイル使用メモリ 79,556 KB
実行使用メモリ 4,504 KB
最終ジャッジ日時 2023-09-20 18:53:53
合計ジャッジ時間 56,015 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 52 ms
4,380 KB
testcase_04 TLE -
testcase_05 AC 1,687 ms
4,380 KB
testcase_06 AC 1,964 ms
4,376 KB
testcase_07 AC 2,565 ms
4,376 KB
testcase_08 AC 3,376 ms
4,376 KB
testcase_09 AC 4,568 ms
4,376 KB
testcase_10 AC 4,486 ms
4,376 KB
testcase_11 AC 4,651 ms
4,376 KB
testcase_12 AC 4,781 ms
4,376 KB
testcase_13 AC 4,841 ms
4,380 KB
testcase_14 AC 4,712 ms
4,380 KB
testcase_15 AC 4,859 ms
4,376 KB
testcase_16 AC 1,800 ms
4,380 KB
testcase_17 AC 1,289 ms
4,380 KB
testcase_18 AC 572 ms
4,376 KB
testcase_19 AC 3,045 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cmath>
#include <iostream>
#include <queue>
#include <list>
#include <map>
#include <numeric>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
#define REP(i,a,n) for(int i=(a); i<(int)(n); i++)
#define rep(i,n) REP(i,0,n)
#define FOR(it,c) for(__typeof((c).begin()) it=(c).begin(); it!=(c).end(); ++it)
#define ALLOF(c) (c).begin(), (c).end()
typedef long long ll;

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

struct ST {
  int a, b;
};
bool operator<(const ST& a, const ST& b){
  if(a.b==b.b) return a.a<b.a;
  return a.b<b.b;
}

int main(){
  ios::sync_with_stdio(false);
  
  int N, a;
  vector<int> ret;
  vector<ST> A;
  cin >> N;
  rep(i,N){
    cin >> a;
    A.push_back((ST){a,a});
  }

  ret.push_back(A[0].a);
  int prev = A[0].a;
  A[0].a = -1;
  rep(i,N){
    int tmp_a = 100000000;
    int tmp_b = 100000000;
    int tmp_i = -1;
    rep(j,N){
      if(A[j].a == -1) continue;
      
      A[j].b = lcm(prev, A[j].a);

      if(tmp_b == A[j].b && tmp_a > A[j].a){
        tmp_a = A[j].a;
        tmp_i = j;
      }else if(tmp_b > A[j].b){
        tmp_b = A[j].b;
        tmp_a = A[j].a;
        tmp_i = j;
      }
    }
    
    if(tmp_i >= 0){
      ret.push_back(tmp_a);
      prev = tmp_a;
      A[tmp_i].a = -1;
    }
  }
  
  rep(i,ret.size()){
    if(i!=0) cout << " ";
    cout << ret[i];
  }
  cout << endl;
  
  return 0;
}
0