O(nLogn + mLogn)时间内存在解。
算法如下:
- 对数组 Y[] 排序。
- 对于 X[] 中的每一个 x,找到大于的最小数字的索引 idx
比 Y[] 中的 x(也称为 x 的 ceil)使用二分查找。
- idx 之后的所有数字都满足关系,所以只需将 (n-idx) 添加到
计数。
这些将是您计划的基本案例:
- 如果 x = 0,则此 x 的对数为 0。
- 如果 x = 1,则此 x 的对数等于 0 数
在 Y[]。
以下情况必须分开处理,因为它们不遵循x小于y意味着x^y大于y^x的一般规则。
- x = 2,y = 3 或 4
- x = 3, y = 2
我不懂 Javascript,所以这里是 C++ 中的代码:
#include<iostream>
#include<algorithm>
using namespace std;
// This function return count of pairs with x as one element
// of the pair. It mainly looks for all values in Y[] where
// x ^ Y[i] > Y[i] ^ x
int count(int x, int Y[], int n, int NoOfY[])
{
// If x is 0, then there cannot be any value in Y such that
// x^Y[i] > Y[i]^x
if (x == 0) return 0;
// If x is 1, then the number of pais is equal to number of
// zeroes in Y[]
if (x == 1) return NoOfY[0];
// Find number of elements in Y[] with values greater than x
// upper_bound() gets address of first greater element in Y[0..n-1]
int* idx = upper_bound(Y, Y + n, x);
int ans = (Y + n) - idx;
// If we have reached here, then x must be greater than 1,
// increase number of pairs for y=0 and y=1
ans += (NoOfY[0] + NoOfY[1]);
// Decrease number of pairs for x=2 and (y=4 or y=3)
if (x == 2) ans -= (NoOfY[3] + NoOfY[4]);
// Increase number of pairs for x=3 and y=2
if (x == 3) ans += NoOfY[2];
return ans;
}
// The main function that returns count of pairs (x, y) such that
// x belongs to X[], y belongs to Y[] and x^y > y^x
int countPairs(int X[], int Y[], int m, int n)
{
// To store counts of 0, 1, 2, 3 and 4 in array Y
int NoOfY[5] = {0};
for (int i = 0; i < n; i++)
if (Y[i] < 5)
NoOfY[Y[i]]++;
// Sort Y[] so that we can do binary search in it
sort(Y, Y + n);
int total_pairs = 0; // Initialize result
// Take every element of X and count pairs with it
for (int i=0; i<m; i++)
total_pairs += count(X[i], Y, n, NoOfY);
return total_pairs;
}
// Driver program to test above functions
int main()
{
int X[] = {2, 1, 6};
int Y[] = {1, 5};
int m = sizeof(X)/sizeof(X[0]);
int n = sizeof(Y)/sizeof(Y[0]);
cout << "Total pairs = " << countPairs(X, Y, m, n);
return 0;
}
Source