📌  相关文章
📜  查询以等边给定长度在等腰三角形上或内部的点的计数

📅  最后修改于: 2021-04-17 18:47:40             🧑  作者: Mango

给定维度N * 2的数组arr [] []表示N个点的坐标,以及由M个整数组成的数组Q [] ,对Q [i]中每个元素的任务是找到点数在每个查询中,位于正坐标轴上的直角等腰三角形的内部或直角等腰三角形上,两个相等的边长为Q [i]

例子:

天真的方法:最简单的方法是在每个查询中遍历点数组并检查它是否位于形成的直角三角形内。完成上述步骤后,打印计数。
时间复杂度: O(N * M)
辅助空间: O(1)

高效的方法:可以基于以下观察来优化上述方法:

请按照以下步骤解决问题:

  • 初始化一个数组,例如max []pre [] ,以存储其坐标之和小于或等于数组索引的点数。
  • 遍历数组arr [] []并将ceil(arr [i] [0] + arr [i] [1])的计数增加1。
  • 计算数组pre []的前缀和数组
  • 遍历数组Q []并打印pre [Q [i]]

下面是上述方法的实现:

C++
// C++ implementation of above approach
 
#include 
using namespace std;
 
int const MAX = 1e6 + 5;
 
// Function to find answer of each query
int query(vector > arr,
          vector Q)
{
    // Stores the count of points with sum
    // less than or equal to their indices
    int pre[MAX] = { 0 };
 
    // Traverse the array
    for (int i = 0; i < arr.size(); i++) {
 
        // If both x and y-coordinate < 0
        if (arr[i][0] < 0 || arr[i][1] < 0)
            continue;
 
        // Stores the sum of co-ordinates
        int sum = ceil((arr[i][0] + arr[i][1]));
 
        // Increment count of sum by 1
        pre[sum]++;
    }
 
    // Prefix array
    for (int i = 1; i < MAX; i++)
        pre[i] += pre[i - 1];
 
    // Perform queries
    for (int i = 0; i < Q.size(); i++) {
        cout << pre[Q[i]] << " ";
    }
    cout << endl;
}
// Drivers Code
int main()
{
    vector > arr = { { 2.1, 3.0 },
                                   { 3.7, 1.2 },
                                   { 1.5, 6.5 },
                                   { 1.2, 0.0 } };
    vector Q = { 2, 8, 5 };
    int N = arr.size();
    int M = Q.size();
 
    query(arr, Q);
}


Java
// Java implementation of above approach
import java.util.*;
class GFG
{
 
static int MAX = (int) (1e6 + 5);
 
// Function to find answer of each query
static void query(double [][]arr,
          int []Q)
{
   
    // Stores the count of points with sum
    // less than or equal to their indices
    int pre[] = new int[MAX];
 
    // Traverse the array
    for (int i = 0; i < arr.length; i++)
    {
 
        // If both x and y-coordinate < 0
        if (arr[i][0] < 0 || arr[i][1] < 0)
            continue;
 
        // Stores the sum of co-ordinates
        int sum = (int) Math.ceil((arr[i][0] + arr[i][1]));
 
        // Increment count of sum by 1
        pre[sum]++;
    }
 
    // Prefix array
    for (int i = 1; i < MAX; i++)
        pre[i] += pre[i - 1];
 
    // Perform queries
    for (int i = 0; i < Q.length; i++)
    {
        System.out.print(pre[Q[i]]+ " ");
    }
    System.out.println();
}
   
// Drivers Code
public static void main(String[] args)
{
    double[][] arr = { { 2.1, 3.0 },
                                   { 3.7, 1.2 },
                                   { 1.5, 6.5 },
                                   { 1.2, 0.0 } };
    int []Q = { 2, 8, 5 };
    int N = arr.length;
    int M = Q.length;
 
    query(arr, Q);
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python3 implementation of above approach
MAX = 10**6 + 5
from math import ceil
 
# Function to find answer of each query
def query(arr, Q):
   
    # Stores the count of points with sum
    # less than or equal to their indices
    pre = [0]*(MAX)
 
    # Traverse the array
    for i in range(len(arr)):
 
        #` If both x and y-coordinate < 0
        if (arr[i][0] < 0 or arr[i][1] < 0):
            continue
 
        # Stores the sum of co-ordinates
        sum = ceil((arr[i][0] + arr[i][1]));
 
        # Increment count of sum by 1
        pre[sum] += 1
 
    # Prefix array
    for i in range(1, MAX):
        pre[i] += pre[i - 1]
 
    # Perform queries
    for i in range(len(Q)):
        print(pre[Q[i]], end = " ")
         
# Drivers Code
if __name__ == '__main__':
    arr = [[ 2.1, 3.0],
          [ 3.7, 1.2],
          [ 1.5, 6.5],
          [ 1.2, 0.0]]
    Q = [2, 8, 5]
    N = len(arr)
    M = len(Q)
 
    query(arr, Q)
 
    # This code is contributed by mohit kumar 29.


C#
// C# implementation of above approach
using System;
public class GFG
{
 
  static int MAX = (int) (1e6 + 5);
 
  // Function to find answer of each query
  static void query(double [,]arr,
                    int []Q)
  {
 
    // Stores the count of points with sum
    // less than or equal to their indices
    int []pre = new int[MAX];
 
    // Traverse the array
    for (int i = 0; i < arr.GetLength(0); i++)
    {
 
      // If both x and y-coordinate < 0
      if (arr[i,0] < 0 || arr[i,1] < 0)
        continue;
 
      // Stores the sum of co-ordinates
      int sum = (int) Math.Ceiling((arr[i,0] + arr[i,1]));
 
      // Increment count of sum by 1
      pre[sum]++;
    }
 
    // Prefix array
    for (int i = 1; i < MAX; i++)
      pre[i] += pre[i - 1];
 
    // Perform queries
    for (int i = 0; i < Q.Length; i++)
    {
      Console.Write(pre[Q[i]]+ " ");
    }
    Console.WriteLine();
  }
 
  // Drivers Code
  public static void Main(String[] args)
  {
    double[,] arr = { { 2.1, 3.0 },
                     { 3.7, 1.2 },
                     { 1.5, 6.5 },
                     { 1.2, 0.0 } };
    int []Q = { 2, 8, 5 };
    int N = arr.GetLength(0);
    int M = Q.Length;
    query(arr, Q);
  }
}
 
// This code is contributed by 29AjayKumar


输出:
1 4 2

时间复杂度: O(N)
辅助空间: O(N)