結果

問題 No.168 ものさし
ユーザー te-sh
提出日時 2018-07-13 13:56:38
言語 D
(dmd 2.081.1)
結果
AC  
実行時間 73 ms
コード長 2,087 Byte
コンパイル時間 1,556 ms
使用メモリ 13,312 KB
最終ジャッジ日時 2018-09-26 03:29:10

テストケース

テストケース表示
入力 結果 実行時間
使用メモリ
99_system_test1.txt AC 17 ms
3,156 KB
challenge01.txt AC 1 ms
1,396 KB
challenge02.txt AC 2 ms
1,396 KB
sample1.txt AC 1 ms
1,396 KB
sample2.txt AC 2 ms
1,396 KB
sample3.txt AC 2 ms
1,396 KB
sample4.txt AC 2 ms
1,396 KB
test1.txt AC 2 ms
1,396 KB
test2.txt AC 2 ms
1,396 KB
test3.txt AC 2 ms
1,436 KB
test4.txt AC 3 ms
1,592 KB
test5.txt AC 15 ms
2,992 KB
test6.txt AC 54 ms
10,956 KB
test7.txt AC 73 ms
13,308 KB
test8.txt AC 72 ms
13,308 KB
test9.txt AC 2 ms
1,400 KB
test10.txt AC 2 ms
1,420 KB
test11.txt AC 3 ms
1,500 KB
test12.txt AC 5 ms
1,772 KB
test13.txt AC 71 ms
13,000 KB
test14.txt AC 73 ms
13,308 KB
test15.txt AC 73 ms
13,308 KB
test16.txt AC 72 ms
13,312 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