给定一个包含N 个不同元素的数组。有M 个查询,每个查询包含一个整数X并要求X在数组中的索引。对于每个查询,任务是从左到右执行线性搜索X ,并计算找到X 所需的比较次数,然后从右到左执行相同的操作。最后,打印所有查询之间双向比较的总数。
例子:
Input: arr[] = {1, 2}, q[] = {1, 2}
Output: 3, 3
For 1-based indexing
For 1st query : Number of comparisons from left to right is 1 and from right to left is 2
For 2nd query : Number of comparisons from left to right is 2 and from right to left is 1
Input: arr[] = {-1, 2, 4, 5, 1}, q[] = {-1, 4, 2}
Output: 3, 7
方法:找到数组中存在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
Javascript
输出:
3 7
时间复杂度: O(N + M)
辅助空间: O(N)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。