Problem A. Farthest Point | Data Structure and Algorithm notes


Problem A. Farthest Point | Data Structure and Algorithm notes
Given a circle on a two-dimentional plane.
Output the integral point in or on the boundary of the circle which has the largest distance from the center.

输入

One line with three floats which are all accurate to three decimal places, indicating the coordinates of the center x, y and the radius r.
For 80% of the data: |x|,|y|<=1000, 1<=r<=1000
For 100% of the data: |x|,|y|<=100000, 1<=r<=100000

输出

One line with two integers separated by one space, indicating the answer.
If there are multiple answers, print the one with the largest x-coordinate.
If there are still multiple answers, print the one with the largest y-coordinate.

样例输入

1.000 1.000 5.000  

样例输出

6 1
题解1 - 圆周枚举
题目要求是返回最大的 x, 所以我们首先根据半径范围将 x 的整数解范围求出来。然后求出可能的 y, 由于题中给出的解有3位小数,如果要精确求解的话,可以将圆方程两边同乘1000,然后判断是否为整数。

自右向左枚举,先枚举圆的上半部分,再枚举圆的下半部分。注意1000的转换。
最坏情况下 O(R).
    private static Point solve(double x0, double y0, double r) {
        // convert double to long(accurate)
        long xl0 = (long)(x0 * 1000), yl0 = (long)(y0 * 1000), rl0 = (long)(r * 1000);
        Point res = new Point(Long.MIN_VALUE, Long.MIN_VALUE);
        int lower_x = (int)Math.ceil(x0 - r), upper_x = (int)Math.floor(x0 + r);
        for (int i = upper_x; i >= lower_x; i--) {
            // circle above
            long y1l = yl0 + (long)(Math.sqrt(rl0*rl0 - (i*1000 - xl0)*(i*1000 - xl0)) + 0.5);
            if ((i*1000 - xl0)*(i*1000 - xl0) + (y1l - yl0)*(y1l - yl0) == rl0*rl0) {
                // ensure y1 is integer
                if (y1l % 1000 == 0) {
                    res.x = i;
                    res.y = y1l / 1000;
                    return res;
                }
            }
            // circle below
            y1l = yl0 - (long)(Math.sqrt(rl0*rl0 - (i*1000 - xl0)*(i*1000 - xl0)) + 0.5);
            if ((i*1000 - xl0)*(i*1000 - xl0) + (y1l - yl0)*(y1l - yl0) == rl0*rl0) {
                // ensure y1 is integer
                if (y1l % 1000 == 0) {
                    res.x = i;
                    res.y = y1l / 1000;
                    return res;
                }
            }
        }
        return res;
    }
题解2 - 整数分解
看似容易实则比较难的一道题,现场通过率非常低。我们仔细审下题,求圆周上的整点,有多个整点时输出最大的 x 和最大的 y. 容易想到的方案是枚举所有可能的 x 和 y, 然后代入等式测试是否相等,这个过不了大的 x 和 y. 如果用开方的方法必然有误差,我用这种方法不知道贡献了多少 WA, 泪流满面... 作为在线测试,更为合理的方案应为先暴力搜索拿到百分之八十的分数。
从 Microsoft 和 Google APAC 在线测试的风格来看是偏向于程序设计竞赛的,那么题目的考点自然就在竞赛范围之内,这道题看似是浮点型的数据,实际上考的却是整数中数论的基础。注意题中的 accurate to three decimal places, 那么也就意味着我们对给定的数据同乘 10^3 后一定是整数!!!这个关键的信息我在测试过程中也没注意到,直到第二天早上醒来后突然就想到了!兴奋地六点多就爬起来了。
首先肯定是要写出圆方程的,设圆心坐标为 (x_0, y_0), 半径为 r, 那么我们有:(x - x_0)^2 + (y - y_0)^2 = r^2
设 m = 10^3(x - x_0)n = 10^3(y - y_0)R = 10^3r, 那么我们有新的圆方程:m^2 + n^2 = R^2其中 m, n, R 均为整数。接下来我们看看给出的数据范围,x, y, r 均是 10^6 以内,那么圆方程两边同乘 10^6 (括号内的数即乘上 10^3)后数据在 10^{18} 以内。我们来估算下整数的范围,2^{10} \approx 10^3, Java 中 int 型为4个字节,最大为 2^{31} - 1 \approx 2 \cdot 10^9, long 型为8个字节,最大为 2^{63} - 1 \approx 2^3 \cdot 10^{18}, 估算下来应该选用 long 保存 m, n, R.
接下来就是数论部分的推导了,先来一个简单的推导,勾股数部分的推导不直观。首先从已知部分出发,已知的只有勾股数方程和 m, n 均是整数,那么接下来肯定是要利用整数的理论无疑了。我们首先对以上圆方程移项开方,考虑到圆的对称性,我们其实只需要考虑圆的八分之一即可。这里考虑0 < m < r部分,m == 0表示在点在轴上,最后单独加上。
m = \sqrt{R^2 - n^2} = \sqrt{(R + n)(R - n)}由于 m 一定是整数,故根号内一定为完全平方数,由于排除了轴上的点,那么-R < n < R, 设G = gcd(R + n, R - n)p^2 = (R + n) / Gq^2 = (R - n) / G, 于是我们有m = Gpqp > q, 由于G 是R + n 和R - n 的最大公约数,故p 和q一定互质,且有:p^2 + q^2 = 2R / G由于p,q 都大于等于1,那么我们能推断出G 一定是 2R 的约数!根据约数(素数)部分的基础理论,我们可以在 O(\sqrt{2R}) 时间内找出所有约数。然后对以上等式进行缩放得到p 的范围,枚举求解,判断p^2q^2 是否互质(最大公约数是否为1)。
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        double xd = in.nextDouble(), yd = in.nextDouble(), rd = in.nextDouble();
        // convert double to long(accurate)
        long x0 = (long)(xd * 1000), y0 = (long)(yd * 1000), r0 = (long)(rd * 1000);
        Point result = solve(x0, y0, r0);
        System.out.println(result.x + " " + result.y);
    }

    private static Point solve(long x0, long y0, long r) {
        Point res = new Point(Long.MIN_VALUE, Long.MIN_VALUE);
        long x_max = Long.MIN_VALUE, y_max = Long.MIN_VALUE;
        // p^2 + q^2 = 2R/divisor, p > q >= 1
        // 1 <= q^2 < R/G < p^2 < 2R/G ==> p >= 2
        List<Long> divisors = getDivisor(r << 1);
        for (long divisor : divisors) {
            long lower = Math.max(2, (long)Math.sqrt(r * 1.0/ divisor));
            // long upper = (long)Math.sqrt(2.0 * r / divisor);
            for (long p = lower; p * p <= 2 * r / divisor; p++) {
                long q = (long)Math.sqrt(2.0 * r / divisor - p * p);
                // check if q is integer
                if (p * p + q * q != 2 * r / divisor) continue;
                // ensure p^2 and q^2 have no common divisor
                if (gcd(p * p, q * q) == 1) {
                    long m = divisor * p * q;
                    long n = r - p * p * divisor;
                    List<Point> points = new ArrayList<Point>();
                    points.add(new Point(m + x0, n + y0));
                    points.add(new Point(m + x0, -1 * n + y0));
                    points.add(new Point(-1 * m + x0, n + y0));
                    points.add(new Point(-1 * m + x0, -1 * n + y0));
                    for (Point point : points) {
                        updateAns(point, res);
                    }
                }
            }
        }

        // axis point check
        List<Point> axis = new ArrayList<Point>();
        axis.add(new Point(x0 + r, y0));
        axis.add(new Point(x0 - r, y0));
        axis.add(new Point(x0, y0 + r));
        axis.add(new Point(x0, y0 - r));
        for (Point point : axis) {
            updateAns(point, res);
        }
        // divide by 1000
        res.x /= 1000;
        res.y /= 1000;

        return res;
    }

    public static void updateAns(Point p, Point res) {
        // point(x, y) in integer
        if ((p.x % 1000 == 0) && (p.y % 1000 == 0)) {
            if (p.x > res.x) {
                res.x = p.x;
                res.y = p.y;
            } else if (p.x == res.x && p.y > res.y) {
                res.y = p.y;
            }
        }
    }

    // enumerate all the divisor for n
    public static List<Long> getDivisor(long n) {
        List<Long> result = new ArrayList<Long>();
        for (long i = 1; i * i <= n; i++) {
            if (n % i == 0) {
                result.add(i);
                // i * i <= n ==> i <= n / i
                if (i != n / i) result.add(n / i);
            }
        }
        Collections.sort(result);
        return result;
    }

    public static long gcd(long a, long b) {
        return (b == 0L) ? a : gcd(b, a % b);
    }
求所有素数时间复杂度 O(\sqrt{n}), 判断是否互质时间复杂度 O(\log n). 枚举最大公约数时间复杂度约 (\sqrt{n}),总的时间复杂度估算应该比 O(n) 小一些,但是小的不明显。所以说,这种方法费了老大劲,但是吃力不讨好!笔试中这种方法极不可取!

题解3 - 勾股数

除了以上使用数论部分整数分解的方法外,还可以巧用勾股数的特性,这种方法需要熟知勾股数的特性。设正整数 m, n, r 满足:m^2 + n^2 = r^2我们对上式两边进行平方可得:(m^2 - n^2)^2 + (2mn)^2 = (m^2 + n^2)^2 = (r^2)^2令 a = m^2 - n^2b = 2mnc = m^2 + n^2. 容易得到:a^2 + b^2 = c^2注意到上述推导可逆,那么也就是说只要我们找到正整数满足m > n就能找到所有可能的勾股数。且根据素勾股数的特性,mn 为一奇一偶,不妨设其为2k-12k. 代入c中可知c4K + 1. 即c % 4 = 1. 根据 Tree of primitive Pythagorean triples 中提到的方法,我们只需找出小于给定的r的素勾股数即可,然后判断是否能整除r.
根据链接中提到的数据结构,使用队列按层次遍历较好,但是空间消耗较大,所以在入队时一定要剪枝。
class Point {
    long x;
    long y;
    Point(long x, long y) {
        this.x = x;
        this.y = y;
    }
}

class Pythagorean {
    long x;
    long y;
    long z;
    Pythagorean(long x, long y, long z) {
        this.x = x;
        this.y = y;
        this.z = z;
    }
}

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        double xd = in.nextDouble(), yd = in.nextDouble(), rd = in.nextDouble();
        // convert double to long(accurate)
        long x0 = (long)(xd * 1000), y0 = (long)(yd * 1000), r0 = (long)(rd * 1000);
        Point result = solve(x0, y0, r0);
        System.out.println(result.x + " " + result.y);
    }

    private static Point solve(long x0, long y0, long r) {
        Point res = new Point(Long.MIN_VALUE, Long.MIN_VALUE);
        long x_max = Long.MIN_VALUE, y_max = Long.MIN_VALUE;
        // init
        Pythagorean pyth0 = new Pythagorean(3, 4, 5);
        Queue<Pythagorean> q = new LinkedList<Pythagorean>();
        q.offer(pyth0);
        boolean update = true;
        while (!q.isEmpty()) {
            int qSize = q.size();
            for (int i = 0; i < qSize; i++) {
                Pythagorean pyth = q.poll();
                if ((r % pyth.z) == 0) {
                    // System.out.println("x = " + pyth.x + ", y = " + pyth.y + ", r = " + pyth.z);
                    long k = r / pyth.z;
                    long kx = k * pyth.x;
                    long ky = k * pyth.y;
                    List<Point> points = new ArrayList<Point>();
                    points.add(new Point(x0 + kx, y0 + ky));
                    points.add(new Point(x0 - kx, y0 + ky));
                    points.add(new Point(x0 + kx, y0 - ky));
                    points.add(new Point(x0 - kx, y0 - ky));
                    if (kx != ky) {
                        points.add(new Point(y0 + ky, x0 + kx));
                        points.add(new Point(y0 - ky, x0 + kx));
                        points.add(new Point(y0 + ky, x0 - kx));
                        points.add(new Point(y0 - ky, x0 - kx));
                    }
                    for (Point point : points) {
                        updateAns(point, res);
                    }
                }
                // add next level Pythagorean
                for (Pythagorean p : nextPyths(pyth)) {
                    if (p.z > r) continue;
                    q.offer(p);
                }
            }
        }

        // axis point check
        List<Point> axis = new ArrayList<Point>();
        axis.add(new Point(x0 + r, y0));
        axis.add(new Point(x0 - r, y0));
        axis.add(new Point(x0, y0 + r));
        axis.add(new Point(x0, y0 - r));
        for (Point point : axis) {
            updateAns(point, res);
        }
        // divide by 1000
        res.x /= 1000;
        res.y /= 1000;

        return res;
    }

    public static List<Pythagorean> nextPyths(Pythagorean pyth) {
        List<Pythagorean> pyths = new ArrayList<Pythagorean>();
        // method 1
        Pythagorean pyth1 = new Pythagorean(0, 0, 0);
        pyth1.x = pyth.x - 2 * pyth.y + 2 * pyth.z;
        pyth1.y = 2 * pyth.x - 1 * pyth.y + 2 * pyth.z;
        pyth1.z = 2 * pyth.x - 2 * pyth.y + 3 * pyth.z;
        pyths.add(pyth1);
        // method 2
        Pythagorean pyth2 = new Pythagorean(0, 0, 0);
        pyth2.x = pyth.x + 2 * pyth.y + 2 * pyth.z;
        pyth2.y = 2 * pyth.x + 1 * pyth.y + 2 * pyth.z;
        pyth2.z = 2 * pyth.x + 2 * pyth.y + 3 * pyth.z;
        pyths.add(pyth2);
        // method 3
        Pythagorean pyth3 = new Pythagorean(0, 0, 0);
        pyth3.x = -1 * pyth.x + 2 * pyth.y + 2 * pyth.z;
        pyth3.y = -2 * pyth.x + pyth.y + 2 * pyth.z;
        pyth3.z = -2 * pyth.x + 2 * pyth.y + 3 * pyth.z;
        pyths.add(pyth3);

        return pyths;
    }

    public static void updateAns(Point p, Point res) {
        // point(x, y) in integer
        if ((p.x % 1000 == 0) && (p.y % 1000 == 0)) {
            if (p.x > res.x) {
                res.x = p.x;
                res.y = p.y;
            } else if (p.x == res.x && p.y > res.y) {
                res.y = p.y;
            }
        }
    }
Read full article from Problem A. Farthest Point | Data Structure and Algorithm notes

Labels

LeetCode (1432) GeeksforGeeks (1122) LeetCode - Review (1067) Review (882) Algorithm (668) to-do (609) Classic Algorithm (270) Google Interview (237) Classic Interview (222) Dynamic Programming (220) DP (186) Bit Algorithms (145) POJ (141) Math (137) Tree (132) LeetCode - Phone (129) EPI (122) Cracking Coding Interview (119) DFS (115) Difficult Algorithm (115) Lintcode (115) Different Solutions (110) Smart Algorithm (104) Binary Search (96) BFS (91) HackerRank (90) Binary Tree (86) Hard (79) Two Pointers (78) Stack (76) Company-Facebook (75) BST (72) Graph Algorithm (72) Time Complexity (69) Greedy Algorithm (68) Interval (63) Company - Google (62) Geometry Algorithm (61) Interview Corner (61) LeetCode - Extended (61) Union-Find (60) Trie (58) Advanced Data Structure (56) List (56) Priority Queue (53) Codility (52) ComProGuide (50) LeetCode Hard (50) Matrix (50) Bisection (48) Segment Tree (48) Sliding Window (48) USACO (46) Space Optimization (45) Company-Airbnb (41) Greedy (41) Mathematical Algorithm (41) Tree - Post-Order (41) ACM-ICPC (40) Algorithm Interview (40) Data Structure Design (40) Graph (40) Backtracking (39) Data Structure (39) Jobdu (39) Random (39) Codeforces (38) Knapsack (38) LeetCode - DP (38) Recursive Algorithm (38) String Algorithm (38) TopCoder (38) Sort (37) Introduction to Algorithms (36) Pre-Sort (36) Beauty of Programming (35) Must Known (34) Binary Search Tree (33) Follow Up (33) prismoskills (33) Palindrome (32) Permutation (31) Array (30) Google Code Jam (30) HDU (30) Array O(N) (29) Logic Thinking (29) Monotonic Stack (29) Puzzles (29) Code - Detail (27) Company-Zenefits (27) Microsoft 100 - July (27) Queue (27) Binary Indexed Trees (26) TreeMap (26) to-do-must (26) 1point3acres (25) GeeksQuiz (25) Merge Sort (25) Reverse Thinking (25) hihocoder (25) Company - LinkedIn (24) Hash (24) High Frequency (24) Summary (24) Divide and Conquer (23) Proof (23) Game Theory (22) Topological Sort (22) Lintcode - Review (21) Tree - Modification (21) Algorithm Game (20) CareerCup (20) Company - Twitter (20) DFS + Review (20) DP - Relation (20) Brain Teaser (19) DP - Tree (19) Left and Right Array (19) O(N) (19) Sweep Line (19) UVA (19) DP - Bit Masking (18) LeetCode - Thinking (18) KMP (17) LeetCode - TODO (17) Probabilities (17) Simulation (17) String Search (17) Codercareer (16) Company-Uber (16) Iterator (16) Number (16) O(1) Space (16) Shortest Path (16) itint5 (16) DFS+Cache (15) Dijkstra (15) Euclidean GCD (15) Heap (15) LeetCode - Hard (15) Majority (15) Number Theory (15) Rolling Hash (15) Tree Traversal (15) Brute Force (14) Bucket Sort (14) DP - Knapsack (14) DP - Probability (14) Difficult (14) Fast Power Algorithm (14) Pattern (14) Prefix Sum (14) TreeSet (14) Algorithm Videos (13) Amazon Interview (13) Basic Algorithm (13) Codechef (13) Combination (13) Computational Geometry (13) DP - Digit (13) LCA (13) LeetCode - DFS (13) Linked List (13) Long Increasing Sequence(LIS) (13) Math-Divisible (13) Reservoir Sampling (13) mitbbs (13) Algorithm - How To (12) Company - Microsoft (12) DP - Interval (12) DP - Multiple Relation (12) DP - Relation Optimization (12) LeetCode - Classic (12) Level Order Traversal (12) Prime (12) Pruning (12) Reconstruct Tree (12) Thinking (12) X Sum (12) AOJ (11) Bit Mask (11) Company-Snapchat (11) DP - Space Optimization (11) Dequeue (11) Graph DFS (11) MinMax (11) Miscs (11) Princeton (11) Quick Sort (11) Stack - Tree (11) 尺取法 (11) 挑战程序设计竞赛 (11) Coin Change (10) DFS+Backtracking (10) Facebook Hacker Cup (10) Fast Slow Pointers (10) HackerRank Easy (10) Interval Tree (10) Limited Range (10) Matrix - Traverse (10) Monotone Queue (10) SPOJ (10) Starting Point (10) States (10) Stock (10) Theory (10) Tutorialhorizon (10) Kadane - Extended (9) Mathblog (9) Max-Min Flow (9) Maze (9) Median (9) O(32N) (9) Quick Select (9) Stack Overflow (9) System Design (9) Tree - Conversion (9) Use XOR (9) Book Notes (8) Company-Amazon (8) DFS+BFS (8) DP - States (8) Expression (8) Longest Common Subsequence(LCS) (8) One Pass (8) Quadtrees (8) Traversal Once (8) Trie - Suffix (8) 穷竭搜索 (8) Algorithm Problem List (7) All Sub (7) Catalan Number (7) Cycle (7) DP - Cases (7) Facebook Interview (7) Fibonacci Numbers (7) Flood fill (7) Game Nim (7) Graph BFS (7) HackerRank Difficult (7) Hackerearth (7) Inversion (7) Kadane’s Algorithm (7) Manacher (7) Morris Traversal (7) Multiple Data Structures (7) Normalized Key (7) O(XN) (7) Radix Sort (7) Recursion (7) Sampling (7) Suffix Array (7) Tech-Queries (7) Tree - Serialization (7) Tree DP (7) Trie - Bit (7) 蓝桥杯 (7) Algorithm - Brain Teaser (6) BFS - Priority Queue (6) BFS - Unusual (6) Classic Data Structure Impl (6) DP - 2D (6) DP - Monotone Queue (6) DP - Unusual (6) DP-Space Optimization (6) Dutch Flag (6) How To (6) Interviewstreet (6) Knapsack - MultiplePack (6) Local MinMax (6) MST (6) Minimum Spanning Tree (6) Number - Reach (6) Parentheses (6) Pre-Sum (6) Probability (6) Programming Pearls (6) Rabin-Karp (6) Reverse (6) Scan from right (6) Schedule (6) Stream (6) Subset Sum (6) TSP (6) Xpost (6) n00tc0d3r (6) reddit (6) AI (5) Abbreviation (5) Anagram (5) Art Of Programming-July (5) Assumption (5) Bellman Ford (5) Big Data (5) Code - Solid (5) Code Kata (5) Codility-lessons (5) Coding (5) Company - WMware (5) Convex Hull (5) Crazyforcode (5) DFS - Multiple (5) DFS+DP (5) DP - Multi-Dimension (5) DP-Multiple Relation (5) Eulerian Cycle (5) Graph - Unusual (5) Graph Cycle (5) Hash Strategy (5) Immutability (5) Java (5) LogN (5) Manhattan Distance (5) Matrix Chain Multiplication (5) N Queens (5) Pre-Sort: Index (5) Quick Partition (5) Quora (5) Randomized Algorithms (5) Resources (5) Robot (5) SPFA(Shortest Path Faster Algorithm) (5) Shuffle (5) Sieve of Eratosthenes (5) Strongly Connected Components (5) Subarray Sum (5) Sudoku (5) Suffix Tree (5) Swap (5) Threaded (5) Tree - Creation (5) Warshall Floyd (5) Word Search (5) jiuzhang (5)

Popular Posts