📜  点数查询位于圆内

📅  最后修改于: 2021-10-23 08:21:15             🧑  作者: Mango

给定 2D 平面上点的n 个坐标 (x, y) 和 Q 查询。每个查询包含一个整数r ,任务是计算位于半径为 r 且以原点为中心的圆的内部或圆周上的点数。
例子 :

Input : n = 5
Coordinates: 
1 1
2 2
3 3
-1 -1
4 4

Query 1: 3
Query 2: 32

Output :
3
5
For first query radius = 3, number of points lie
inside or on the circumference are (1, 1), (-1, -1),
(2, 2). There are only 3 points lie inside or on 
the circumference of the circle.
For second query radius = 32, all five points are
inside the circle. 

以原点 (0, 0) 为圆心,半径为 r, x 2 + y 2 = r 2的圆的方程。并且条件 (x 1 , y 1 ) 位于圆周内或圆周上,x 1 2 + y 1 2 <= r 2
Naive 方法可以针对每个查询,遍历所有点并检查条件。这需要 O(n*Q) 时间复杂度。
一种有效的方法是为每个点坐标预先计算 x 2 + y 2并将它们存储在数组 p[] 中。现在,对数组 p[] 进行排序。然后对数组应用二分搜索以找到每个查询的条件 p[i] <= r 2 的最后一个索引。
下面是这个方法的实现:

C++
// C++ program to find number of points lie inside or
// on the cirumference of circle for Q queries.
#include 
using namespace std;
 
// Computing the x^2 + y^2 for each given points
// and sorting them.
void preprocess(int p[], int x[], int y[], int n)
{
    for (int i = 0; i < n; i++)
        p[i] = x[i] * x[i] + y[i] * y[i];
 
    sort(p, p + n);
}
 
// Return count of points lie inside or on circumference
// of circle using binary search on p[0..n-1]
int query(int p[], int n, int rad)
{
    int start = 0, end = n - 1;
    while ((end - start) > 1) {
        int mid = (start + end) / 2;
        double tp = sqrt(p[mid]);
 
        if (tp > (rad * 1.0))
            end = mid - 1;
        else
            start = mid;
    }
 
    double tp1 = sqrt(p[start]), tp2 = sqrt(p[end]);
 
    if (tp1 > (rad * 1.0))
        return 0;
    else if (tp2 <= (rad * 1.0))
        return end + 1;
    else
        return start + 1;
}
 
// Driven Program
int main()
{
    int x[] = { 1, 2, 3, -1, 4 };
    int y[] = { 1, 2, 3, -1, 4 };
    int n = sizeof(x) / sizeof(x[0]);
 
    // Compute distances of all points and keep
    // the distances sorted so that query can
    // work in O(logn) using Binary Search.
    int p[n];
    preprocess(p, x, y, n);
 
    // Print number of points in a circle of radius 3.
    cout << query(p, n, 3) << endl;
 
    // Print number of points in a circle of radius 32.
    cout << query(p, n, 32) << endl;
    return 0;
}


Java
// JAVA Code for Queries on count of
// points lie inside a circle
import java.util.*;
 
class GFG {
 
    // Computing the x^2 + y^2 for each given points
    // and sorting them.
    public static void preprocess(int p[], int x[],
                                  int y[], int n)
    {
        for (int i = 0; i < n; i++)
            p[i] = x[i] * x[i] + y[i] * y[i];
 
        Arrays.sort(p);
    }
 
    // Return count of points lie inside or on
    // circumference of circle using binary
    // search on p[0..n-1]
    public static int query(int p[], int n, int rad)
    {
        int start = 0, end = n - 1;
        while ((end - start) > 1) {
            int mid = (start + end) / 2;
            double tp = Math.sqrt(p[mid]);
 
            if (tp > (rad * 1.0))
                end = mid - 1;
            else
                start = mid;
        }
 
        double tp1 = Math.sqrt(p[start]);
        double tp2 = Math.sqrt(p[end]);
 
        if (tp1 > (rad * 1.0))
            return 0;
        else if (tp2 <= (rad * 1.0))
            return end + 1;
        else
            return start + 1;
    }
 
    /* Driver program to test above function */
    public static void main(String[] args)
    {
        int x[] = { 1, 2, 3, -1, 4 };
        int y[] = { 1, 2, 3, -1, 4 };
        int n = x.length;
 
        // Compute distances of all points and keep
        // the distances sorted so that query can
        // work in O(logn) using Binary Search.
        int p[] = new int[n];
        preprocess(p, x, y, n);
 
        // Print number of points in a circle of
        // radius 3.
        System.out.println(query(p, n, 3));
 
        // Print number of points in a circle of
        // radius 32.
        System.out.println(query(p, n, 32));
    }
}
// This code is contributed by Arnav Kr. Mandal.


Python 3
# Python 3 program to find number of
# points lie inside or on the cirumference
# of circle for Q queries.
import math
 
# Computing the x^2 + y^2 for each
# given points and sorting them.
def preprocess(p, x, y, n):
    for i in range(n):
        p[i] = x[i] * x[i] + y[i] * y[i]
 
    p.sort()
 
# Return count of points lie inside
# or on circumference of circle using
# binary search on p[0..n-1]
def query(p, n, rad):
 
    start = 0
    end = n - 1
    while ((end - start) > 1):
        mid = (start + end) // 2
        tp = math.sqrt(p[mid])
 
        if (tp > (rad * 1.0)):
            end = mid - 1
        else:
            start = mid
 
    tp1 = math.sqrt(p[start])
    tp2 = math.sqrt(p[end])
 
    if (tp1 > (rad * 1.0)):
        return 0
    elif (tp2 <= (rad * 1.0)):
        return end + 1
    else:
        return start + 1
 
# Driver Code
if __name__ == "__main__":
     
    x = [ 1, 2, 3, -1, 4 ]
    y = [ 1, 2, 3, -1, 4 ]
    n = len(x)
 
    # Compute distances of all points and keep
    # the distances sorted so that query can
    # work in O(logn) using Binary Search.
    p = [0] * n
    preprocess(p, x, y, n)
 
    # Print number of points in a
    # circle of radius 3.
    print(query(p, n, 3))
 
    # Print number of points in a
    # circle of radius 32.
    print(query(p, n, 32))
 
# This code is contributed by ita_c


C#
// C# Code for Queries on count of
// points lie inside a circle
using System;
 
class GFG {
 
    // Computing the x^2 + y^2 for each
    // given points and sorting them.
    public static void preprocess(int[] p, int[] x,
                                    int[] y, int n)
    {
        for (int i = 0; i < n; i++)
            p[i] = x[i] * x[i] + y[i] * y[i];
 
        Array.Sort(p);
    }
 
    // Return count of points lie inside or on
    // circumference of circle using binary
    // search on p[0..n-1]
    public static int query(int[] p, int n, int rad)
    {
        int start = 0, end = n - 1;
        while ((end - start) > 1) {
            int mid = (start + end) / 2;
            double tp = Math.Sqrt(p[mid]);
 
            if (tp > (rad * 1.0))
                end = mid - 1;
            else
                start = mid;
        }
 
        double tp1 = Math.Sqrt(p[start]);
        double tp2 = Math.Sqrt(p[end]);
 
        if (tp1 > (rad * 1.0))
            return 0;
        else if (tp2 <= (rad * 1.0))
            return end + 1;
        else
            return start + 1;
    }
 
    /* Driver program to test above function */
    public static void Main()
    {
        int[] x = { 1, 2, 3, -1, 4 };
        int[] y = { 1, 2, 3, -1, 4 };
        int n = x.Length;
 
        // Compute distances of all points and keep
        // the distances sorted so that query can
        // work in O(logn) using Binary Search.
        int[] p = new int[n];
        preprocess(p, x, y, n);
 
        // Print number of points in a circle of
        // radius 3.
        Console.WriteLine(query(p, n, 3));
 
        // Print number of points in a circle of
        // radius 32.
        Console.WriteLine(query(p, n, 32));
    }
}
 
// This code is contributed by vt_m.


Javascript


输出:

3
5

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程