📌  相关文章
📜  查询位于等腰三角形上或内的点的计数,等腰三角形的给定长度

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

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

例子:

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

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

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

  • 初始化一个数组,比如max size 的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


Javascript


输出:
1 4 2

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

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