結果
| 問題 |
No.168 ものさし
|
| ユーザー |
|
| 提出日時 | 2018-07-13 13:56:38 |
| 言語 | D (dmd 2.109.1) |
| 結果 |
AC
|
| 実行時間 | 103 ms / 2,000 ms |
| コード長 | 2,087 bytes |
| コンパイル時間 | 863 ms |
| コンパイル使用メモリ | 118,828 KB |
| 実行使用メモリ | 14,968 KB |
| 最終ジャッジ日時 | 2024-06-13 01:29:44 |
| 合計ジャッジ時間 | 2,462 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 19 |
ソースコード
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);
}
}