結果

問題 No.14 最小公倍数ソート
ユーザー simansiman
提出日時 2022-02-08 13:57:02
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,245 bytes
コンパイル時間 5,238 ms
コンパイル使用メモリ 108,344 KB
実行使用メモリ 7,184 KB
最終ジャッジ日時 2023-09-05 11:44:04
合計ジャッジ時間 2,724 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
4,988 KB
testcase_01 AC 4 ms
4,804 KB
testcase_02 AC 5 ms
5,132 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cassert>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <climits>
#include <map>
#include <queue>
#include <set>
#include <cstring>
#include <vector>

using namespace std;
typedef long long ll;

struct Node {
  int v;
  int cnt;

  Node(int v = -1, int cnt = -1) {
    this->v = v;
    this->cnt = cnt;
  }

  bool operator>(const Node &n) const {
    return cnt < n.cnt;
  }
};

int main() {
  int N;
  cin >> N;
  vector<int> A;
  vector<int> counter(10010, 0);
  set<int> memo[10010];

  for (int i = 0; i < N; ++i) {
    int a;
    cin >> a;
    counter[a]++;
    A.push_back(a);

    for (int j = a; j <= 10000; j += a) {
      memo[j].insert(a);
    }
  }

  int cur = A[0];
  counter[cur]--;
  vector<int> ans;
  ans.push_back(cur);

  for (int i = 0; i < N - 1; ++i) {
    bool ok = true;

    for (int j = cur; j <= 10000 && ok; j += cur) {
      if (memo[j].empty()) continue;

      for (int v : memo[j]) {
        if (counter[v] == 0) continue;

        ans.push_back(v);
        counter[v]--;
        cur = v;
        ok = false;
        break;
      }
    }
  }

  for (int i = 0; i < N; ++i) {
    cout << ans[i];
    if (i + 1 < N) cout << " ";
  }
  cout << endl;

  return 0;
}
0