题解 | #浇花#

浇花

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);
    }
}

全部评论

相关推荐

不愿透露姓名的神秘牛友
10-12 10:48
已编辑
秋招之苟:邻居家老哥19届双2硕大厂开发offer拿遍了,前几天向他请教秋招,他给我看他当年的简历,0实习实验室项目技术栈跟开发基本不沾边😂,我跟他说这个放在现在中厂简历都过不了
点赞 评论 收藏
分享
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务