結果

問題 No.14 最小公倍数ソート
ユーザー PachicobuePachicobue
提出日時 2017-06-04 04:37:10
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 58 ms / 5,000 ms
コード長 2,542 bytes
コンパイル時間 1,788 ms
コンパイル使用メモリ 179,948 KB
実行使用メモリ 9,468 KB
最終ジャッジ日時 2023-10-22 03:21:39
合計ジャッジ時間 3,253 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,392 KB
testcase_01 AC 2 ms
4,392 KB
testcase_02 AC 2 ms
4,392 KB
testcase_03 AC 6 ms
4,892 KB
testcase_04 AC 58 ms
9,468 KB
testcase_05 AC 30 ms
7,272 KB
testcase_06 AC 33 ms
7,540 KB
testcase_07 AC 38 ms
8,020 KB
testcase_08 AC 44 ms
8,588 KB
testcase_09 AC 52 ms
9,168 KB
testcase_10 AC 52 ms
9,124 KB
testcase_11 AC 54 ms
9,320 KB
testcase_12 AC 54 ms
9,304 KB
testcase_13 AC 53 ms
9,356 KB
testcase_14 AC 53 ms
9,268 KB
testcase_15 AC 55 ms
9,416 KB
testcase_16 AC 31 ms
7,472 KB
testcase_17 AC 26 ms
6,944 KB
testcase_18 AC 17 ms
6,092 KB
testcase_19 AC 42 ms
8,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define RFOR(i, a, b) for (ll i = (b)-1; i >= (a); i--)
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define rep1(i, n) for (ll i = 1; i <= (n); i++)
#define rrep(i, n) for (ll i = (n)-1; i >= 0; i--)

#define pb push_back
#define mp make_pair
#define fst first
#define snd second
#define show(x) cout << #x << " = " << x << endl
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define pii pair<int, int>
#define vi vector<int>

using namespace std;
template <class S, class T>
ostream& operator<<(ostream& o, const pair<S, T>& p)
{
    return o << "(" << p.first << "," << p.second << ")";
}
template <class T>
ostream& operator<<(ostream& o, const vector<T>& vc)
{
    o << "sz = " << vc.size() << endl
      << "[";
    for (const T& v : vc)
        o << v << ",";
    o << "]";
    return o;
}
using ll = long long;
constexpr ll MOD = 1000000007;

constexpr int MAX = 10000;
int a[MAX];
vector<int> divs[MAX];
set<pii> mul[MAX + 1];

int main()
{
    int n;
    cin >> n;
    rep(i, n)
    {
        cin >> a[i];
        for (int d = 1; d * d <= a[i]; d++) {
            if (a[i] % d == 0) {
                if (d * d == a[i]) {
                    divs[i].pb(d);
                    mul[d].insert(mp(a[i], i));
                } else {
                    divs[i].pb(d);
                    divs[i].pb(a[i] / d);
                    mul[d].insert(mp(a[i], i));
                    mul[a[i] / d].insert(mp(a[i], i));
                }
            }
        }
    }

    vector<int> ans(n);
    ans[0] = 0;
    rep1(i, n - 1)
    {
        int prev = ans[i - 1];
        for (int d : divs[prev]) {
            mul[d].erase(mp(a[prev], prev));
        }
        int mini = numeric_limits<int>::max();
        int min_m = -1;

        for (const int d : divs[prev]) {
            if (not mul[d].empty()) {
                const auto e = *(mul[d].begin());
                const int m = e.first;
                const int m_ind = e.second;
                if (mini > a[prev] * m / d) {
                    mini = a[prev] * m / d;
                    min_m = m_ind;
                } else if (mini == a[prev] * m / d) {
                    if (a[min_m] > a[m_ind]) {
                        min_m = m_ind;
                    }
                }
            }
        }
        ans[i] = min_m;
    }
    rep(i, n)
    {
        if (i > 0) {
            cout << " ";
        }
        cout << a[ans[i]];
    }
    cout << endl;
    return 0;
}
0