📜  线性搜索中m个查询在每个方向上的比较数

📅  最后修改于: 2021-04-21 20:45:19             🧑  作者: Mango

给定一个包含N个不同元素的数组。有M个查询,每个查询包含一个整数X,并询问数组中X的索引。对于每个查询,任务是从左到右执行线性搜索X ,并计算找到X并从右到左执行相同操作所需的比较次数。最后,打印所有查询之间双向比较的总数。

例子:

方法:找到数组中存在X的索引,即i (基于1的索引),从左到右的比较次数为i ,从右到左的比较次数为(n – i + 1 ) 。我们需要做的就是快速找到索引。可以通过使用一个映射来完成,其中键是元素的值,而值是索引。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function to return the count of comparisons from left to right
// and right to left in linear search among m queries
pair countCamparisions(int n, int arr[], int m, int qry[])
{
    int i;
    unordered_map index;
    for (i = 1; i <= n; i++) {
  
        // arr[i] occurs at i
        index[arr[i]] = i;
    }
  
    // Count of comparisons for left to right and right to left
    int ltr = 0, rtl = 0;
    for (i = 1; i <= m; i++) {
        int x = qry[i];
        ltr += index[x];
        rtl += n - index[x] + 1;
    }
    return make_pair(ltr, rtl);
}
  
// Driver Code
int main()
{
    // -1 will be ignored as it is 1-based indexing
    int arr[] = { -1, 2, 4, 5, 1 };
    int n = (sizeof(arr) / sizeof(arr[0])) - 1;
  
    int q[] = { -1, 4, 2 };
    int m = (sizeof(q) / sizeof(q[0])) - 1;
  
    pair res = countCamparisions(n, arr, m, q);
    cout << res.first << " " << res.second;
}


Java
// Java implementation of the approach
import java.util.HashMap;
import java.util.Map;
  
class GFG
{
  
// Function to return the count of 
// comparisons from left to right 
// and right to left in linear 
// search among m queries 
static Pair countCamparisions(int n, 
                            int arr[], int m, int qry[]) 
{ 
    int i; 
    HashMap index = new HashMap<>(); 
    for (i = 1; i <= n; i++) 
    { 
  
        // arr[i] occurs at i 
        index.put(arr[i], i); 
    } 
  
    // Count of comparisons for left
    // to right and right to left 
    int ltr = 0, rtl = 0; 
    for (i = 1; i <= m; i++)
    { 
        int x = qry[i]; 
        ltr += index.get(x); 
        rtl += n - index.get(x) + 1; 
    } 
      
    Pair ans = new Pair<>(ltr, rtl);
    return ans; 
}
  
    // Driver Code 
    public static void main(String []args)
    {
          
        // -1 will be ignored as it is 1-based indexing 
        int arr[] = { -1, 2, 4, 5, 1 }; 
        int n = arr.length - 1; 
      
        int q[] = { -1, 4, 2 }; 
        int m = q.length - 1; 
      
        Pair res = countCamparisions(n, arr, m, q); 
        System.out.println(res.first + " " + res.second);
    }
}
  
class Pair 
{
    A first;
    B second;
  
    public Pair(A first, B second)
    {
        this.first = first;
        this.second = second;
    }
}
      
// This code is contributed by Rituraj Jain


Python3
# Python 3 implementation of the
# above approach
  
# Function to return the count of 
# comparisons from left to right 
# and right to left in linear search
# among m queries 
def countCamparisions(n, arr, m, qry) :
  
    index = {}
    for i in range(1, n + 1) :
  
        # arr[i] occurs at i 
        index[arr[i]] = i
      
    # Count of comparisons for left to 
    # right and right to left 
    ltr, rtl = 0, 0
    for i in range(1, m + 1) :
        x = qry[i]
        ltr += index[x] 
        rtl += n - index[x] + 1
      
    return (ltr, rtl) 
  
# Driver Code
if __name__ == "__main__" :
  
    # -1 will be ignored as it 
    # is 1-based indexing 
    arr = [ -1, 2, 4, 5, 1 ] 
    n = len(arr) - 1
  
    q = [ -1, 4, 2 ] 
    m = len(q) - 1
  
    res = countCamparisions(n, arr, m, q) 
    print(res[0], res[1]) 
      
# This code is contributed by Ryuga


C#
// C# implementation of the approach
using System;
using System.Collections.Generic;
  
class GFG
{
  
// Function to return the count of 
// comparisons from left to right 
// and right to left in linear 
// search among m queries 
static Pair countCamparisions(int n, int []arr, 
                                   int m, int []qry) 
{ 
    int i; 
    Dictionary index = new Dictionary(); 
    for (i = 1; i <= n; i++) 
    { 
  
        // arr[i] occurs at i 
        index.Add(arr[i], i); 
    } 
  
    // Count of comparisons for left
    // to right and right to left 
    int ltr = 0, rtl = 0; 
    for (i = 1; i <= m; i++)
    { 
        int x = qry[i]; 
        ltr += index[x]; 
        rtl += n - index[x] + 1; 
    } 
      
    Pair ans = new Pair(ltr, rtl);
    return ans; 
}
  
// Driver Code 
public static void Main(String []args)
{
      
    // -1 will be ignored as 
    // it is 1-based indexing 
    int []arr = { -1, 2, 4, 5, 1 }; 
    int n = arr.Length - 1; 
  
    int []q = { -1, 4, 2 }; 
    int m = q.Length - 1; 
  
    Pair res = countCamparisions(n, arr, m, q); 
    Console.WriteLine(res.first + " " + res.second);
}
}
  
class Pair 
{
    public A first;
    public B second;
      
    public Pair(A first, B second)
    {
        this.first = first;
        this.second = second;
    }
}
  
// This code is contributed by 29AjayKumar


输出:
3 7