結果

問題 No.168 ものさし
ユーザー te-shte-sh
提出日時 2018-07-13 13:56:38
言語 D
(dmd 2.107.1)
結果
AC  
実行時間 106 ms / 2,000 ms
コード長 2,087 bytes
コンパイル時間 706 ms
コンパイル使用メモリ 106,304 KB
実行使用メモリ 14,924 KB
最終ジャッジ日時 2023-09-03 20:44:08
合計ジャッジ時間 2,887 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 23 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 3 ms
4,380 KB
testcase_11 AC 21 ms
5,360 KB
testcase_12 AC 74 ms
13,568 KB
testcase_13 AC 105 ms
14,680 KB
testcase_14 AC 105 ms
14,672 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 2 ms
4,384 KB
testcase_18 AC 6 ms
4,380 KB
testcase_19 AC 100 ms
14,404 KB
testcase_20 AC 106 ms
14,600 KB
testcase_21 AC 104 ms
14,924 KB
testcase_22 AC 105 ms
14,604 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

auto rdsp(){return readln.splitter;}
void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;}
void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);}
void readC(T...)(size_t n,ref T t){foreach(ref v;t)v=new typeof(v)(n);foreach(i;0..n){auto r=rdsp;foreach(ref v;t)pick(r,v[i]);}}

void main()
{
  int n; readV(n);
  long[] x, y; readC(n, x, y);

  struct P { int s, t; long d; }
  P[] p;
  foreach (i; 0..n)
    foreach (j; i+1..n)
      p ~= P(i, j, (x[i]-x[j])^^2+(y[i]-y[j])^^2);

  p.sort!"a.d<b.d";

  auto uf = UnionFind!int(n);
  foreach (pi; p) {
    uf.unite(pi.s, pi.t);
    if (uf.isSame(0, n-1)) {
      auto bs = iota(10, int.max.to!long, 10).map!(l => tuple(l, pi.d <= l^^2)).assumeSorted!"a[1]<b[1]";
      writeln(bs.upperBound(tuple(0, false)).front[0]);
      return;
    }
  }
}

pure T nsqrt(T)(T n)
{
  import std.algorithm, std.conv, std.range, core.bitop;
  if (n <= 1) return n;
  T m = T(1) << (n.bsr/2+1);
  return iota(1, m).map!"a * a".assumeSorted!"a <= b".lowerBound(n).length.to!T;
}

struct UnionFind(T)
{
  import std.algorithm, std.range;

  T[] p; // parent
  const T s; // sentinel
  const T n;
  T countForests; // number of forests
  T[] countNodes; // number of nodes in forests

  this(T n)
  {
    this.n = n;
    s = n;
    p = new T[](n);
    p[] = s;
    countForests = n;
    countNodes = new T[](n);
    countNodes[] = 1;
  }

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

  bool unite(T i, T j)
  {
    auto pi = this[i], pj = this[j];
    if (pi != pj) {
      p[pj] = pi;
      --countForests;
      countNodes[pi] += countNodes[pj];
      return true;
    } else {
      return false;
    }
  }

  auto countNodesOf(T i) { return countNodes[this[i]]; }
  bool isSame(T i, T j) { return this[i] == this[j]; }

  auto groups()
  {
    auto g = new T[][](n);
    foreach (i; 0..n) g[this[i]] ~= i;
    return g.filter!(l => !l.empty);
  }
}
0