結果

問題 No.168 ものさし
ユーザー te-shte-sh
提出日時 2017-02-06 17:46:44
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 79 ms / 2,000 ms
コード長 1,544 bytes
コンパイル時間 690 ms
コンパイル使用メモリ 100,120 KB
実行使用メモリ 12,156 KB
最終ジャッジ日時 2023-09-03 01:22:15
合計ジャッジ時間 2,519 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 24 ms
6,192 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,384 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 4 ms
4,376 KB
testcase_11 AC 15 ms
5,388 KB
testcase_12 AC 46 ms
10,304 KB
testcase_13 AC 63 ms
11,016 KB
testcase_14 AC 63 ms
12,156 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 3 ms
4,380 KB
testcase_18 AC 6 ms
4,380 KB
testcase_19 AC 76 ms
12,084 KB
testcase_20 AC 78 ms
12,088 KB
testcase_21 AC 79 ms
11,356 KB
testcase_22 AC 79 ms
11,540 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.algorithm, std.conv, std.range, std.stdio, std.string;
import std.math;      // math functions

alias Point!long point;

void main()
{
  auto n = readln.chomp.to!size_t;
  auto pi = n.iota.map!(_ => readln.split.to!(int[])).map!(rd => point(rd[0], rd[1])).array;

  auto dij = new long[][](n, n);
  auto maxD = long(0);
  foreach (i; n.iota)
    foreach (j; n.iota) {
      dij[i][j] = (pi[i] - pi[j]).hypot2;
      maxD = max(maxD, dij[i][j]);
    }

  auto calc(long _, long a) {
    auto uf = new UnionFind!size_t(n);
    foreach (i; n.iota)
      foreach (j; n.iota)
        if (dij[i][j] <= a * a)
          uf.unite(i, j);
    return uf.find(0) == uf.find(n - 1);
  }

  auto r = iota(10L, maxD.to!real.sqrt.to!long + 20, 10L).assumeSorted!calc.upperBound(0);
  writeln(r.front);
}

struct Point(T) {
  T x, y;

  auto opBinary(string op: "+")(Point!T rhs) { return Point!T(x + rhs.x, y + rhs.y); }
  auto opBinary(string op: "-")(Point!T rhs) { return Point!T(x - rhs.x, y - rhs.y); }
  auto opBinary(string op: "*")(Point!T rhs) { return x * rhs.x + y * rhs.y; }
  auto opBinary(string op: "*")(T a) { return Point!T(x * a, y * a); }

  T hypot2() { return x ^^ 2 + y ^^ 2; }
}

class UnionFind(T) {
  T[] p; // parent
  T s; // sentinel

  this(T n) {
    p = new T[](n);
    s = n + 1;
    p[] = s;
  }

  T find(T i) {
    if (p[i] == s) {
      return i;
    } else {
      p[i] = find(p[i]);
      return p[i];
    }
  }

  void unite(T i, T j) {
    auto pi = find(i), pj = find(j);
    if (pi != pj) p[pj] = pi;
  }
}
0