結果

問題 No.96 圏外です。
ユーザー te-shte-sh
提出日時 2017-04-21 11:23:16
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 569 ms / 5,000 ms
コード長 2,836 bytes
コンパイル時間 2,120 ms
コンパイル使用メモリ 166,780 KB
実行使用メモリ 193,080 KB
最終ジャッジ日時 2023-09-03 12:55:14
合計ジャッジ時間 8,093 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 25 ms
38,072 KB
testcase_05 AC 44 ms
41,396 KB
testcase_06 AC 49 ms
44,696 KB
testcase_07 AC 30 ms
17,668 KB
testcase_08 AC 63 ms
50,648 KB
testcase_09 AC 71 ms
49,052 KB
testcase_10 AC 67 ms
19,020 KB
testcase_11 AC 117 ms
52,056 KB
testcase_12 AC 147 ms
73,040 KB
testcase_13 AC 132 ms
24,000 KB
testcase_14 AC 182 ms
61,268 KB
testcase_15 AC 199 ms
29,112 KB
testcase_16 AC 242 ms
60,120 KB
testcase_17 AC 268 ms
77,276 KB
testcase_18 AC 301 ms
75,652 KB
testcase_19 AC 297 ms
68,196 KB
testcase_20 AC 569 ms
193,080 KB
testcase_21 AC 280 ms
81,844 KB
testcase_22 AC 397 ms
17,712 KB
testcase_23 AC 402 ms
17,360 KB
testcase_24 AC 2 ms
4,380 KB
testcase_25 AC 211 ms
75,604 KB
testcase_26 AC 252 ms
77,896 KB
testcase_27 AC 224 ms
77,516 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

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;

  if (n == 0) {
    writeln(1);
    return;
  }

  auto minX = pi.map!"a.x".minElement;
  auto minY = pi.map!"a.y".minElement;
  foreach (ref p; pi) {
    p.x -= minX;
    p.y -= minY;
  }

  auto maxX = pi.map!"a.x".maxElement, maxBx = maxX / 10;
  auto maxY = pi.map!"a.y".maxElement, maxBy = maxY / 10;

  auto bucket = new size_t[][][](maxBy + 1, maxBx + 1);
  foreach (i, p; pi)
    bucket[p.y / 10][p.x / 10] ~= i;

  auto uf = UnionFind!size_t(n);
  foreach (i, p; pi)
    for (auto by = p.y / 10 - 1; by <= p.y / 10 + 1; ++by)
      for (auto bx = p.x / 10 - 1; bx <= p.x / 10 + 1; ++bx) {
        if (bx < 0 || bx > maxBx || by < 0 || by > maxBy) continue;
        foreach (ti; bucket[by][bx]) {
          if (i >= ti || (p - pi[ti]).hypot2 > 100) continue;
          uf.unite(i, ti);
        }
      }

  auto gi = new size_t[][](n);
  foreach (i; 0..n) gi[uf.find(i)] ~= i;

  auto gpi = gi.filter!"!a.empty".map!(g => pi.indexed(g).array.convexHull);
  auto maxD = 0;
  foreach (gp; gpi)
    foreach (i; 0..gp.length)
      foreach (j; i+1..gp.length)
        maxD = max(maxD, (gp[i] - gp[j]).hypot2);

  writefln("%.7f", maxD.to!real.sqrt + 2);
}

struct UnionFind(T)
{
  T[] p; // parent
  const 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;
  }
}

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

alias Point!int point;

Point!T[] convexHull(T)(Point!T[] pi)
{
  if (pi.length <= 2) return pi;

  auto cross(Point!T a, Point!T b, Point!T o)
  {
    return (a.x - o.x) * (b.y - o.y) - (a.y - o.y) * (b.x - o.x);
  }

  pi.sort!"a.x == b.x ? a.y < b.y : a.x < b.x";

  Point!T[] lower;
  foreach (p; pi) {
    while (lower.length >= 2 && cross(lower[$-2], lower[$-1], p) <= 0) lower.length -= 1;
    lower ~= p;
  }

  Point!T[] upper;
  foreach_reverse (p; pi) {
    while (upper.length >= 2 && cross(upper[$-2], upper[$-1], p) <= 0) upper.length -= 1;
    upper ~= p;
  }

  return (lower.dropBackOne ~ upper.dropBackOne).array;
}
0