結果

問題 No.168 ものさし
ユーザー kenkooookenkoooo
提出日時 2015-03-20 01:10:33
言語 Java21
(openjdk 21)
結果
AC  
実行時間 206 ms / 2,000 ms
コード長 2,016 bytes
コンパイル時間 2,406 ms
コンパイル使用メモリ 75,676 KB
実行使用メモリ 71,684 KB
最終ジャッジ日時 2023-08-25 20:48:32
合計ジャッジ時間 5,819 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 114 ms
57,152 KB
testcase_01 AC 43 ms
49,328 KB
testcase_02 AC 42 ms
49,380 KB
testcase_03 AC 43 ms
49,820 KB
testcase_04 AC 43 ms
49,360 KB
testcase_05 AC 42 ms
49,592 KB
testcase_06 AC 42 ms
49,288 KB
testcase_07 AC 43 ms
49,336 KB
testcase_08 AC 43 ms
49,556 KB
testcase_09 AC 51 ms
49,536 KB
testcase_10 AC 62 ms
50,884 KB
testcase_11 AC 102 ms
56,688 KB
testcase_12 AC 178 ms
63,716 KB
testcase_13 AC 190 ms
71,436 KB
testcase_14 AC 201 ms
71,268 KB
testcase_15 AC 43 ms
49,420 KB
testcase_16 AC 50 ms
49,668 KB
testcase_17 AC 54 ms
49,484 KB
testcase_18 AC 82 ms
51,804 KB
testcase_19 AC 179 ms
64,856 KB
testcase_20 AC 198 ms
71,684 KB
testcase_21 AC 205 ms
71,596 KB
testcase_22 AC 206 ms
71,160 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.IOException;
import java.util.PriorityQueue;

public class Main {
	public static void main(String[] args) {
		int N = nextInt();
		int[] x = new int[N];
		int[] y = new int[N];
		for (int i = 0; i < N; i++) {
			x[i] = nextInt();
			y[i] = nextInt();
		}

		PriorityQueue<Edge> priorityQueue = new PriorityQueue<>();
		UnionFind uFind = new UnionFind(N);

		for (int i = 0; i < N; i++) {
			for (int j = 0; j < i; j++) {
				long dx = x[i] - x[j];
				long dy = y[i] - y[j];
				long distSq = dx * dx + dy * dy;
				long d = (long) Math.sqrt(distSq);
				while (distSq > d * d) {
					d++;
				}

				d = (d + 9) / 10 * 10;
				priorityQueue.add(new Edge(i, j, (int) d));
			}
		}

		int max = 0;
		while (!uFind.isSame(0, N - 1)) {
			Edge edge = priorityQueue.poll();
			if (!uFind.isSame(edge.from, edge.to)) {
				uFind.unite(edge.from, edge.to);
				max = Math.max(max, edge.weight);
			}
		}
		System.out.println(max);

	}

	static int nextInt() {
		int c;
		try {
			c = System.in.read();
			while (c != '-' && (c < '0' || c > '9'))
				c = System.in.read();
			if (c == '-')
				return -nextInt();
			int res = 0;
			while (c >= '0' && c <= '9') {
				res = res * 10 + c - '0';
				c = System.in.read();
			}
			return res;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return -1;
	}
}

class UnionFind {
	int[] parts;

	UnionFind(int n) {
		parts = new int[n];
		for (int i = 0; i < n; i++)
			parts[i] = i;
	}

	public int find(int x) {
		if (parts[x] == x)
			return x;
		return parts[x] = find(parts[x]);
	}

	public Boolean isSame(int x, int y) {
		return find(x) == find(y);
	}

	public void unite(int x, int y) {
		if (find(x) == find(y))
			return;
		parts[find(x)] = find(y);
	}
}

class Edge implements Comparable<Edge> {
	int weight;
	int from, to;

	Edge(int f, int t, int w) {
		this.weight = w;
		this.from = f;
		this.to = t;
	}

	@Override
	public int compareTo(Edge edge) {
		// 昇順
		return this.weight - edge.weight;
	}
}
0