結果

問題 No.14 最小公倍数ソート
ユーザー te-shte-sh
提出日時 2017-02-08 17:15:29
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 3,149 ms / 5,000 ms
コード長 663 bytes
コンパイル時間 3,182 ms
コンパイル使用メモリ 101,936 KB
実行使用メモリ 4,568 KB
最終ジャッジ日時 2023-09-03 01:26:57
合計ジャッジ時間 38,354 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,384 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 34 ms
4,380 KB
testcase_04 AC 3,149 ms
4,376 KB
testcase_05 AC 1,052 ms
4,376 KB
testcase_06 AC 1,232 ms
4,376 KB
testcase_07 AC 1,611 ms
4,376 KB
testcase_08 AC 2,118 ms
4,384 KB
testcase_09 AC 2,879 ms
4,380 KB
testcase_10 AC 2,832 ms
4,568 KB
testcase_11 AC 2,928 ms
4,380 KB
testcase_12 AC 3,002 ms
4,380 KB
testcase_13 AC 3,046 ms
4,384 KB
testcase_14 AC 2,968 ms
4,380 KB
testcase_15 AC 3,064 ms
4,380 KB
testcase_16 AC 1,141 ms
4,376 KB
testcase_17 AC 816 ms
4,380 KB
testcase_18 AC 364 ms
4,376 KB
testcase_19 AC 1,928 ms
4,384 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
/dmd2/linux/bin64/../../src/phobos/std/numeric.d(2999): Warning: cannot inline function `std.numeric.gcdImpl!uint.gcdImpl`

ソースコード

diff #

import std.algorithm, std.conv, std.range, std.stdio, std.string;
import std.numeric;   // gcd

void main()
{
  auto n = readln.chomp.to!size_t;
  auto ai = readln.split.to!(int[]);

  auto bi = [ai.front]; ai = ai.drop(1);

  while (!ai.empty) {
    auto i = calcMin(ai, bi.back);
    bi ~= ai[i];
    ai = ai.remove(i);
  }

  writeln(bi.to!(string[]).join(" "));
}

auto calcMin(Range)(Range ai, int b)
{
  auto minLcm = int.max, minA = 0, minIndex = size_t(0);
  foreach (i, a; ai) {
    auto lcm = a / gcd(a, b) * b;
    if (minLcm > lcm || minLcm == lcm && minA > a) {
      minLcm = lcm;
      minA = a;
      minIndex = i;
    } 
  }
  return minIndex;
}
0