我有 N 个数字 a[1..N] 和 2 个其他整数 L 和 H。我如何计算满足 i < j < k 和 L <= a[i] + 的元组 (i,j,k) 的数量a[j] + a[k] <= H。
1 <= T <= 100
1 <= N <= 1000
1 <= L <= H <= 1000000
1 <= a[i] <= 1000000
PS:需要比 N2logn 更好的解决方案
解决方案
由于我的 C/C++ 有点生疏,这主要是一个算法问题,所以我将用伪代码编写(主要是正确的 C/C++,其中包含一些需要一段时间才能写出的算法)。
如果您至少有 sizeof(int)*10^12 字节的内存和可用时间,则可以使用时间复杂度为 O(n^2 * log(n)) 的此算法。
// Sort the N numbers using your favorite, efficient sorting method. (Quicksort, mergesort, etc.) [O(n*log(n))].
int[] b = sort(a)
int[] c = int[length(b)^2];
// Compute the sums of all of the numbers (O(n^2))
for(int i = 0; i < length(b); i++){
for (int j = i; j < length(b); j++){
c[i*length(b)+j] = b[i]+b[j];
}
}
// Sort the sum list (you can do the sorts in-place if you are comfortable) - O(n^2*log(n))
d = sort(c);
// For each number in your list, grab the list of of sums so that L<=num+sum<=H O(n)
// Use binary search to find the lower, upper bounds O(log(n))
// (Total complexity for this part: O(n*log(n))
int total = 0;
for (int i = 0; i < b; i++){
int min_index = binary_search(L-b[i]); // search for largest number <= L-b[i]
int max_index = binary_search(H-b[i]); // search for smallest number >= H-b[i]
total += max_index - min_index + 1; // NOTE: This does not handle edge cases like not finding any sums that work
}
return total;
int find_three(int arr[], int c, int l,int h)
{
int i, j, e, s, k;
int count =0;
sort(arr,arr+c);
c--;
while(arr[c]>h)
c--;
int sum=0;
for (int i = 0; i<=c-2;i++)
{ sum=arr[i]+arr[i+1]+arr[i+2];
if(sum>h)
break;
for(j=i+1;j<=c-1;j++)
{
for(k=j+1;k<=c;k++)
{ sum=arr[i]+arr[j]+arr[k];
if(sum>=l &&sum<=h)
count++;
if(sum>h)
break;
}
if(sum>h)
break;
}
}
return count;
}
一个基本的方法:
for (i=0; i<N; i++) {
for (j=i+1; j<N; j++) {
for (k=j+1; k<N; k++) {
int sum = a[i] + a[j] + a[k];
if (L <= sum && sum <= H) number_of_tuples++;
}
}
}
可能更好(可能有一个错误,但基本想法是如果你已经超过最大值就打破):
for (i=0; i<N; i++) {
if (a[i] > H) continue;
for (j=i+1; j<N; j++) {
if (a[i] + a[j] > H) continue;
for (k=j+1; k<N; k++) {
int sum = a[i] + a[j] + a[k];
if (L <= sum && sum <= H) number_of_tuples++;
}
}
}