結果

問題 No.14 最小公倍数ソート
ユーザー PachicobuePachicobue
提出日時 2017-06-04 04:25:27
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,420 bytes
コンパイル時間 1,956 ms
コンパイル使用メモリ 182,496 KB
実行使用メモリ 9,524 KB
最終ジャッジ日時 2023-10-22 03:21:34
合計ジャッジ時間 4,217 ms
ジャッジサーバーID
(参考情報)
judge9 / judge10
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
4,448 KB
testcase_01 AC 3 ms
4,448 KB
testcase_02 AC 3 ms
4,448 KB
testcase_03 WA -
testcase_04 AC 79 ms
9,524 KB
testcase_05 AC 40 ms
7,328 KB
testcase_06 AC 43 ms
7,596 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 AC 73 ms
9,224 KB
testcase_10 WA -
testcase_11 AC 82 ms
9,376 KB
testcase_12 AC 73 ms
9,360 KB
testcase_13 WA -
testcase_14 AC 74 ms
9,324 KB
testcase_15 AC 73 ms
9,472 KB
testcase_16 AC 40 ms
7,528 KB
testcase_17 AC 31 ms
7,000 KB
testcase_18 AC 19 ms
6,148 KB
testcase_19 AC 52 ms
8,432 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));
                }
            }
        }
        sort(divs[i].begin(), divs[i].end());
    }

    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 > prev * m / d) {
                    mini = prev * m / d;
                    min_m = m_ind;
                }
            }
        }
        ans[i] = min_m;
    }
    rep(i, n)
    {
        if (i > 0) {
            cout << " ";
        }
        cout << a[ans[i]];
    }
    cout << endl;
    return 0;
}
0