题解 | #浇花#

浇花

https://www.nowcoder.com/practice/1eaabd93f16946f28c5917b3e9d9df48

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        int n = in.nextInt();
        int x1 = in.nextInt();
        int y1 = in.nextInt();
        int x2 = in.nextInt();
        int y2 = in.nextInt();

        int[] x = new int[n];
        int[] y = new int[n];

        for (int i = 0; i < n; i++) {
            x[i] = in.nextInt();
            y[i] = in.nextInt();
        }

        long r1 = 0;
        long r2 = 0;
        long min_sum = Long.MAX_VALUE;

        for (int i = 0; i < n; i++) {
            r1 = distance(x[i], y[i], x1, y1);
            r2 = 0;

            for (int j = 0; j < n; j++) {
                if (distance(x[j], y[j], x1, y1) > r1) {
                    r2 = Math.max(r2, distance(x[j], y[j], x2, y2));
                }
            }

            min_sum = Math.min(min_sum, r1 + r2);
        }

        r2 = 0;
        for (int i = 0; i < n; i++) {
            r2 = Math.max(r2, distance(x[i], y[i], x2, y2));
        }
        min_sum = Math.min(min_sum, r2);

        System.out.println(min_sum);
    }

    private static long distance(int x1, int y1, int x2, int y2) {
        return (long)(x1 - x2) * (x1 - x2) + (long)(y1 - y2) * (y1 - y2);
    }
}

全部评论

相关推荐

点赞 评论 收藏
分享
offer多多的六边形战士很无语:看了你的博客,感觉挺不错的,可以把你的访问量和粉丝数在简历里提一下,闪光点(仅个人意见)
点赞 评论 收藏
分享
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务