📌  相关文章
📜  查询以检查数组中是否存在任何对,其值最多等于给定对

📅  最后修改于: 2021-04-29 09:56:39             🧑  作者: Mango

在数组Queries []中,以成对形式给定arr []Q个查询对的向量,每个查询的任务是检查是否存在两个值均小于当前查询对中的值的对。如果发现是真的,则打印“是” 。否则,打印“否”

例子:

天真的方法:最简单的方法是遍历数组Queries [] []并遍历每个对,并检查给定的对对,并检查是否存在这样的对,其对应值大于或等于对{p1,p2 }然后打印“是”。否则,打印“否”
时间复杂度: O(N * K)
辅助空间: O(1)

高效方法:为了优化上述方法,我们的想法是使用二进制搜索。请按照以下步骤解决此问题:

  • 相对于对中的第一个元素,按升序对对数组进行排序。如果存在两个对,它们的第一个值相同,那么将基于该对的第二个元素来排列对。
  • 排序后,遍历对的数组,对于具有相同第一值的所有对,将第二值替换为具有相同第一值的所有对中的最小值。
  • 现在,遍历给定的Queries []数组并对数组arr []中的每一对执行二进制搜索。
  • 如果通过上述步骤获得的对小于Query []中的当前对,则打印“是”,否则打印“否”

下面是上述方法的实现:

C++14
// C++ program for the above approach
#include 
using namespace std;
 
// Function that performs binary search
// to find value less than or equal to
// first value of the pair
int binary_search(vector > vec,
                  int n, int a)
{
    int low, high, mid;
    low = 0;
    high = n - 1;
 
    // Perform binary search
    while (low < high) {
        // Find the mid
        mid = low + (high - low + 1) / 2;
 
        // Update the high
        if (vec[mid].first > a) {
            high = mid - 1;
        }
 
        // Else update low
        else if (vec[mid].first <= a) {
            low = mid;
        }
    }
 
    // Return the low index
    return low;
}
 
// Function to modify the second
// value of each pair
void modify_vec(
    vector >& v, int n)
{
    // start from index 1
    for (int i = 1; i < n; i++) {
        v[i].second
            = min(v[i].second,
                  v[i - 1].second);
    }
}
 
// Function to evaluate each query
int evaluate_query(vector > v,
                   int n, int m1,
                   int m2)
{
    // Find value less than or equal to
    // the first value of pair
    int temp = binary_search(v, n, m1);
 
    // check if we got the required
    // pair or not
    if ((v[temp].first <= m1)
        && (v[temp].second <= m2)) {
        return 1;
    }
 
    return 0;
}
 
// Function to find a pair whose values is
// less than the given pairs in query
void checkPairs(vector >& v,
                vector >& queries)
{
 
    // Find the size of the vector
    int n = v.size();
 
    // sort the vector based on
    // the first value
    sort(v.begin(), v.end());
 
    // Function Call to modify the
    // second value of each pair
    modify_vec(v, n);
 
    int k = queries.size();
 
    // Traverse each queries
    for (int i = 0; i < k; i++) {
        int m1 = queries[i].first;
        int m2 = queries[i].second;
 
        // Evaluate each query
        int result
            = evaluate_query(v, n, m1, m2);
 
        // Print the result
        if (result > 0)
            cout << "Yes\n";
        else
            cout << "No\n";
    }
}
 
// Driver Code
int main()
{
    vector > arr
        = { { 3, 5 }, { 2, 7 }, { 2, 3 }, { 4, 9 } };
 
    vector > queries
        = { { 3, 4 }, { 3, 2 }, { 4, 1 }, { 3, 7 } };
 
    // Function Call
    checkPairs(arr, queries);
 
    return 0;
}


Java
// Java program for above approach
import java.util.*;
import java.lang.*;
class GFG{
 
  // Function that performs binary search
  // to find value less than or equal to
  // first value of the pair
  static int binary_search(int[][] vec,
                           int n, int a)
  {
    int low, high, mid;
    low = 0;
    high = n - 1;
 
    // Perform binary search
    while (low < high)
    {
 
      // Find the mid
      mid = low + (high - low + 1) / 2;
 
      // Update the high
      if (vec[mid][0] > a)
      {
        high = mid - 1;
      }
 
      // Else update low
      else if (vec[mid][1] <= a)
      {
        low = mid;
      }
    }
 
    // Return the low index
    return low;
  }
 
  // Function to modify the second
  // value of each pair
  static void modify_vec(
    int[][] v, int n)
  {
    // start from index 1
    for (int i = 1; i < n; i++)
    {
      v[i][1] =  Math.min(v[i][1],
                          v[i - 1][1]);
    }
  }
 
  // Function to evaluate each query
  static int evaluate_query(int[][] v,
                            int n, int m1,
                            int m2)
  {
 
    // Find value less than or equal to
    // the first value of pair
    int temp = binary_search(v, n, m1);
 
    // check if we got the required
    // pair or not
    if ((v[temp][0] <= m1)
        && (v[temp][1] <= m2))
    {
      return 1;
    }
 
    return 0;
  }
 
  // Function to find a pair whose values is
  // less than the given pairs in query
  static void checkPairs(int[][] v,
                         int[][] queries)
  {
 
    // Find the size of the vector
    int n = v.length;
 
    // sort the vector based on
    // the first value
    Arrays.sort(v, (a, b)->a[0]-b[0]);
 
    // Function Call to modify the
    // second value of each pair
    modify_vec(v, n);
    int k = queries.length;
 
    // Traverse each queries
    for (int i = 0; i < k; i++)
    {
      int m1 = queries[i][0];
      int m2 = queries[i][1];
 
      // Evaluate each query
      int result
        = evaluate_query(v, n, m1, m2);
 
      // Print the result
      if (result > 0)
        System.out.println("Yes");
      else
        System.out.println("No");
    }
  }
 
 
  // Driver function
  public static void main (String[] args)
  {
    int[][] arr
      = { { 3, 5 }, { 2, 7 }, { 2, 3 }, { 4, 9 } };
 
    int[][] queries
      = { { 3, 4 }, { 3, 2 }, { 4, 1 }, { 3, 7 } };
 
    // Function Call
    checkPairs(arr, queries);
  }
}
 
// This code is contributed by offbeat


Python3
# Python3 program for the above approach
 
# Function that performs binary search
# to find value less than or equal to
# first value of the pair
def binary_search(vec, n, a):
    low, high, mid = 0, 0, 0
    low = 0
    high = n - 1
 
    # Perform binary search
    while (low < high):
 
      # Find the mid
        mid = low + (high - low + 1) // 2
 
        # Update the high
        if (vec[mid][0] > a):
            high = mid - 1
         
        # Else update low
        elif vec[mid][0] <= a:
            low = mid
 
    # Return the low index
    return low
 
# Function to modify the second
# value of each pair
def modify_vec(v, n):
     
    # start from index 1
    for i in range(n):
        v[i][1] = min(v[i][1], v[i - 1][1])
 
    return v
 
# Function to evaluate each query
def evaluate_query(v, n, m1, m2):
 
    # Find value less than or equal to
    # the first value of pair
    temp = binary_search(v, n, m1)
 
    # check if we got the required
    # pair or not
    if ((v[temp][0] <= m1)
        and (v[temp][1] <= m2)):
        return 1
 
    return 0
 
# Function to find a pair whose values is
# less than the given pairs in query
def checkPairs(v, queries):
 
    # Find the size of the vector
    n = len(v)
 
    # sort the vector based on
    # the first value
    v = sorted(v)
 
    # Function Call to modify the
    # second value of each pair
    v = modify_vec(v, n)
 
    k = len(queries)
 
    # Traverse each queries
    for i in range(k):
        m1 = queries[i][0]
        m2 = queries[i][1]
 
        # Evaluate each query
        result = evaluate_query(v, n, m1, m2)
 
        # Prthe result
        if (result > 0):
            print("Yes")
        else:
            print("No")
 
# Driver Code
if __name__ == '__main__':
    arr= [ [ 3, 5 ], [ 2, 7 ], [ 2, 3 ], [ 4, 9 ] ]
 
    queries = [ [ 3, 4 ], [ 3, 2 ], [ 4, 1 ], [ 3, 7 ] ]
 
    # Function Call
    checkPairs(arr, queries)
 
# This code is contributed by mohit kumar 29


输出:
Yes
No
No
Yes

时间复杂度: O(Q * log N)
辅助空间: O(1)