結果

問題 No.168 ものさし
ユーザー nebukuro09nebukuro09
提出日時 2016-09-27 16:41:04
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 851 ms / 2,000 ms
コード長 1,795 bytes
コンパイル時間 494 ms
コンパイル使用メモリ 61,780 KB
実行使用メモリ 10,856 KB
最終ジャッジ日時 2023-08-25 21:03:51
合計ジャッジ時間 4,579 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 69 ms
7,168 KB
testcase_01 AC 82 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 599 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 851 ms
4,376 KB
testcase_08 AC 539 ms
4,376 KB
testcase_09 AC 3 ms
4,376 KB
testcase_10 AC 4 ms
4,380 KB
testcase_11 AC 18 ms
7,172 KB
testcase_12 AC 53 ms
9,900 KB
testcase_13 AC 74 ms
10,548 KB
testcase_14 AC 73 ms
10,564 KB
testcase_15 AC 122 ms
4,376 KB
testcase_16 AC 97 ms
4,376 KB
testcase_17 AC 85 ms
4,376 KB
testcase_18 AC 52 ms
4,768 KB
testcase_19 AC 68 ms
10,504 KB
testcase_20 AC 67 ms
10,552 KB
testcase_21 AC 70 ms
10,548 KB
testcase_22 AC 71 ms
10,856 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <vector>
#include <iostream>
using namespace std;

class UnionFind {
public:
  vector<int> table;
  UnionFind(int n) {
    table = vector<int> (n, -1);
  }
  
  ~UnionFind(){};
  
  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y) return;
    if (table[x] >= table[y]) {
      table[y] += table[x];
      table[x] = y;
    } else {
      table[x] += table[y];
      table[y] = x;
    }
  }

  int find(int x) {
    if (table[x] < 0) return x;
    return table[x] = find(table[x]);  // 根を張り替えながら再帰的に根ノードを探す
  }
};

int N;
long long int dists[1001][1001];
long long int f(long long int x0, long long int y0, long long int x1, long long int y1) {
  return (x0-x1)*(x0-x1) + (y0-y1)*(y0-y1);
}
bool ok(long long int m) {
  UnionFind uf = UnionFind(N);
  for (int i = 0; i < N; i++)
    for (int j = i+1; j < N; j++)
      if (dists[i][j] <= m) {
	uf.unite(i, j);
      }
  return uf.find(0) == uf.find(N-1);
}
long long int sqrtint(long long int n) {
  long long int i;
  for (i = 0; n > 0; i++)
    n -= i * 2 + 1;
  return i-1;
}
  
int main() {
  long long int x[1000];
  long long int y[1000];
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> x[i] >> y[i];
  }

  long long int high = 1000000000000000000*3;
  long long int low = 0;
  long long int  middle;

  for (int i = 0; i < N; i++) 
    for (int j = i+1; j < N; j++)
      dists[i][j] = f(x[i], y[i], x[j], y[j]);

  while (high-low > 1) {
    middle = (high+low)/2;
    if (ok(middle))
      high = middle;
    else
      low = middle;
  }

  if (!ok(high))
    high = low;
  long long int sqrt = sqrtint(high);
  if (sqrt*sqrt < high)
    sqrt++;

  long long int ans = sqrt/10*10;
  if (sqrt%10 > 0)
    ans += 10;
  cout << ans << endl;
  return 0;
}
0