要点
- 要求输出半径平方和,是个整数,求距离时无需开根号,
- 先把所有点按照到第一个雷达的距离降序排列,并且初始时全都归到第一个雷达下,则此时两个雷达的半径平方分别为$r_1 = point[0].d_1$、$r_2=0$,记录下当前的半径平方和$res = r_1 + r_2$
- 依次把第一个雷达覆盖范围内最远的点(记为$point[i]$)归到另一个雷达下,更新变化后两个雷达的距离$r_1 = point[i+1].d_1$、$r_2 = max(r_2, point[i].d_2)$,并计算新的半径平方和,与原来的对比取较小值,$res = min(res, r_1 + r_2)$
- 整个过程$r_1$不断缩小、$r_2$可能增大也可能不变
- 感觉AC代码好像少了$r_1=0,r_2=max(point[i].d_2)$的情况,是数据问题还是本来就不用考虑
代码
import java.util.*;
import java.lang.*;
public class Main {
static Scanner scanner = new Scanner(System.in);
static int n;
static class Dpoint {
public int d1, d2;
public Dpoint(int d1, int d2) {
this.d1 = d1;
this.d2 = d2;
}
}
static int getDistance(int x1, int y1, int x2, int y2) {
int dx = x1 - x2, dy = y1 - y2;
return dx * dx + dy * dy;
}
public static void main(String[] args) {
int x1 = scanner.nextInt(), y1 = scanner.nextInt();
int x2 = scanner.nextInt(), y2 = scanner.nextInt();
ArrayList<Dpoint> ps = new ArrayList<>();
n = scanner.nextInt();
for (int i = 1; i <= n; i++) {
int x = scanner.nextInt(), y = scanner.nextInt();
int d1 = getDistance(x1, y1, x, y), d2 = getDistance(x2, y2, x, y);
ps.add(new Dpoint(d1, d2));
}
ps.sort((o1, o2) -> o2.d1 - o1.d1);
int r1 = ps.get(0).d1, r2 = 0, res = r1 + r2;
for (int i = 1; i < ps.size(); i++) {
Dpoint cur = ps.get(i), pre = ps.get(i - 1);
r1 = cur.d1;
r2 = Math.max(r2, pre.d2);
res = Math.min(res, r1 + r2);
}
System.out.println(res);
}
}