結果

問題 No.168 ものさし
ユーザー te-shte-sh
提出日時 2016-09-07 16:56:05
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 332 ms / 2,000 ms
コード長 1,539 bytes
コンパイル時間 787 ms
コンパイル使用メモリ 124,136 KB
実行使用メモリ 30,480 KB
最終ジャッジ日時 2023-09-02 21:59:05
合計ジャッジ時間 4,377 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 73 ms
6,192 KB
testcase_01 AC 1 ms
4,372 KB
testcase_02 AC 1 ms
4,368 KB
testcase_03 AC 2 ms
4,372 KB
testcase_04 AC 1 ms
4,368 KB
testcase_05 AC 1 ms
4,368 KB
testcase_06 AC 2 ms
4,372 KB
testcase_07 AC 2 ms
4,372 KB
testcase_08 AC 2 ms
4,372 KB
testcase_09 AC 4 ms
4,368 KB
testcase_10 AC 8 ms
4,368 KB
testcase_11 AC 55 ms
5,952 KB
testcase_12 AC 181 ms
16,032 KB
testcase_13 AC 261 ms
30,480 KB
testcase_14 AC 260 ms
29,880 KB
testcase_15 AC 1 ms
4,372 KB
testcase_16 AC 3 ms
4,372 KB
testcase_17 AC 6 ms
4,372 KB
testcase_18 AC 17 ms
4,372 KB
testcase_19 AC 319 ms
30,128 KB
testcase_20 AC 332 ms
30,200 KB
testcase_21 AC 331 ms
29,388 KB
testcase_22 AC 331 ms
29,836 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.algorithm, std.array, std.container, std.range, std.bitmanip;
import std.numeric, std.math, std.bigint, std.random, core.bitop;
import std.string, std.regex, std.conv, std.stdio, std.typecons;

class UnionFind {
  size_t[] par;

  this(size_t n) {
    par = new size_t[](n);
    par[] = size_t.max;
  }

  size_t find(size_t i) {
    if (par[i] == size_t.max) {
      return i;
    } else {
      par[i] = find(par[i]);
      return par[i];
    }
  }

  void unite(size_t i, size_t j) {
    auto pi = find(i), pj = find(j);
    if (pi != pj) par[pj] = pi;
  }
}

struct point {
  long x, y;

  point opBinary(string op)(point rhs) {
    static if (op == "+") return point(x + rhs.x, y + rhs.y);
    else static if (op == "-") return point(x - rhs.x, y - rhs.y);
  }
}

alias Tuple!(size_t, "i", size_t, "j", long, "len") bet;

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

  bet[] bi;
  foreach (i; 0..n - 1)
    foreach (j; i + 1..n)
      bi ~= bet(i, j, rulerLen(pi[i], pi[j]));
  bi.sort!("a.len < b.len");

  auto uf = new UnionFind(n);
  foreach (b; bi) {
    uf.unite(b.i, b.j);
    if (uf.find(0) == uf.find(n - 1)) {
      writeln(b.len);
      break;
    }
  }
}

long rulerLen(point p1, point p2)
{
  auto p = p1 - p2;
  auto a = p.x ^^ 2 + p.y ^^ 2;

  bool calc(long _, long b) {
    return b ^^ 2 >= a;
  }

  return iota(10L, a.to!real.sqrt.to!long * 2, 10L).assumeSorted!(calc).upperBound(0).front;
}
0