結果

問題 No.14 最小公倍数ソート
ユーザー parukiparuki
提出日時 2018-02-21 09:13:50
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 53 ms / 5,000 ms
コード長 1,808 bytes
コンパイル時間 1,736 ms
コンパイル使用メモリ 174,728 KB
実行使用メモリ 9,128 KB
最終ジャッジ日時 2023-10-12 13:29:18
合計ジャッジ時間 3,817 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,352 KB
testcase_01 AC 3 ms
4,352 KB
testcase_02 AC 2 ms
4,356 KB
testcase_03 AC 5 ms
4,632 KB
testcase_04 AC 53 ms
9,128 KB
testcase_05 AC 24 ms
6,804 KB
testcase_06 AC 26 ms
7,112 KB
testcase_07 AC 32 ms
7,612 KB
testcase_08 AC 39 ms
8,220 KB
testcase_09 AC 46 ms
8,644 KB
testcase_10 AC 46 ms
8,596 KB
testcase_11 AC 48 ms
8,928 KB
testcase_12 AC 50 ms
8,776 KB
testcase_13 AC 49 ms
8,800 KB
testcase_14 AC 51 ms
8,804 KB
testcase_15 AC 49 ms
9,008 KB
testcase_16 AC 26 ms
7,060 KB
testcase_17 AC 22 ms
6,628 KB
testcase_18 AC 15 ms
5,756 KB
testcase_19 AC 36 ms
7,920 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include "bits/stdc++.h"
using namespace std;
#define FOR(i,j,k) for(int (i)=(j);(i)<(int)(k);++(i))
#define rep(i,j) FOR(i,0,j)
#define each(x,y) for(auto &(x):(y))
#define mp make_pair
#define MT make_tuple
#define all(x) (x).begin(),(x).end()
#define debug(x) cout<<#x<<": "<<(x)<<endl
#define smax(x,y) (x)=max((x),(y))
#define smin(x,y) (x)=min((x),(y))
#define MEM(x,y) memset((x),(y),sizeof (x))
#define sz(x) (int)(x).size()
#define RT return
#define vv(a,b,c,d) vector<vector<a> >(b,vector<a>(c,d))
#define vvv(a,b,c,d,e) vector<vector<vector<a> > >(b,vv(a,c,d,e))
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
using vll = vector<ll>;

vector<int> divisors(int n) {
    vector<int> res;
    for (int i = 1; (long long)i*i <= n; ++i) {
        if (n%i == 0) {
            res.push_back(i);
            int j = n / i;
            if (j != i)res.push_back(j);
        }
    }
    return res;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << fixed << setprecision(20);
    
    int N;
    cin >> N;
    multiset<int> S[10001];
    vi ds[10001];

    vi a(N), p(N);
    rep(i, N) {
        cin >> a[i];
        ds[a[i]] = divisors(a[i]);
        sort(all(ds[a[i]]));
        each(d, ds[a[i]])S[d].insert(a[i]);
    }
    p[0] = a[0];
    rep(i, N - 1) {
        int b = p[i];
        each(d, ds[b]) {
            auto it = S[d].find(b);
            S[d].erase(it);
        }
        int c = INT_MAX, mi = INT_MAX;
        each(g, ds[b]) if(sz(S[g])){
            int x = *S[g].begin();
            int l = b * x / g;
            if (l < mi || (l == mi && x < c)) {
                mi = l;
                c = x;
            }
        }
        p[i + 1] = c;
    }

    rep(i, N)cout << p[i] << (i != N - 1 ? ' ' : '\n');
}
0