简体   繁体   English

从两个数组的总和中找到(n * logn)个最小元素

[英]finding (n*logn) minimal elements from sum of two arrays

I have some problem in my homework, I'm given two arrays A,B (they are sorted) with n elements each one, I need to find (n*logn) minimal elements (or n if it is possible) from the array which cosists of elements which is sum of one element from array A and one element from array B, total number of elemnts in C will be n^2 , - C = {a+b | a belongs to A, b belongs to B} 我在作业中遇到了一些问题,我给了两个数组A,B(它们被排序),每个数组都有n个元素,我需要从数组中查找(n * logn个)最小元素(如果可能的话,则为n个)元素由数组A中的一个元素与数组B中的一个元素之和组成,则C中元素的total number of elemnts in C will be n^2C = {a+b | a belongs to A, b belongs to B} C = {a+b | a belongs to A, b belongs to B} , I need to make it with complexity O(n*logn). C = {a+b | a belongs to A, b belongs to B} ,我需要使其复杂度为O(n * logn)。
Thanks in advance for any help! 在此先感谢您的帮助!

PS I tried to solve it but I have some problems. PS我试图解决它,但是我有一些问题。 I know that the first element will be a1 + b1 , a1 first element from A, b1 first element from B. 我知道第一个元素将是a1 + b1 ,A中的a1第一个元素,B中的b1第一个元素。

edited 编辑

Also all elements in C are different. 同样,C中的所有元素都不同。

this code below will do the trick it uses the hint you gave 2 elements are sorted and it's order is number of elements needed to find: 下面的代码将利用您给您的提示进行技巧,将2个元素排序,其顺序是查找所需元素的数量:

int m = n *lon(n);
a[n] = max_int;
b[n] = max_int;

for(int i=0,j=0;i+j<m;)
{
    if(a[i+1]+b[j] < a[i]+b[j+1])
    {
        c[i+j] = a[i+1]+b[j];
        i++;
    }
    else
    {
        c[i+j] = a[i]+b[j+1];
        j++;
    }
}

I'm not giving you the answer, but here are a few hints to get you thinking in the right direction. 我没有给您答案,但这是一些提示,可帮助您朝正确的方向思考。

So let's say you have. 所以说你有。 Keep in mind that these lists are already sorted, this is very important. 请记住,这些列表已经排序,这非常重要。 {a1,a2,a3} {b1,b2,b3} {a1,a2,a3} {b1,b2,b3}

As you correctly said, the first element is a1+b1. 就像您正确说的那样,第一个元素是a1 + b1。 Why? 为什么? Because those are the smallest numbers in both arrays, so their sum will be the smallest. 因为这是两个数组中最小的数字,所以它们的和将是最小的。

Now what options do you have for the second element? 现在您对第二个元素有什么选择? Remember that the list is sorted! 请记住,列表已排序! Could it be a1+b2? 可能是a1 + b2吗? Could it be a1+b3? 可能是a1 + b3吗? And then for the opposite, Could it be b1+a2? 相反,是b1 + a2吗? Could it be b1+a3? 可能是b1 + a3吗?

Could it be b2+a2? 可能是b2 + a2吗? (two yes answers and three no answers above) (上面有两个肯定答案,三个没有答案)

Using this you should be able to figure out how you find the second smallest element and from here it should be easy to find how to do that for all elements. 使用它,您应该能够弄清楚如何找到第二个最小的元素,并且从这里开始,应该很容易找到如何对所有元素执行该操作。

If you are still confused, ask questions in the comments. 如果您仍然感到困惑,请在评论中提出问题。

GL! GL!

Find the smallest element in A (lets call it a) and the smallest element in B (b). 在A中找到最小的元素(叫作a),在B中找到最小的元素(b)。 The smallest element in C is a + b. C中的最小元素是a + b。

If you need the position of that element in C and if first element is a1 + b1, second a1 + b2 etc. the position of wanted element will be pos(a) * size(B) + pos(b) . 如果需要该元素在C中的位置,并且第一个元素是a1 + b1,第二个a1 + b2等,则所需元素的位置将是pos(a) * size(B) + pos(b)

This is a common homework problem. 这是一个常见的家庭作业问题。 Since the A and B arrays are sorted, you can do a pairwise merge, almost like merge sort, the only difference being that you are trying to compare the sums, not the numbers themselves. 由于对A和B数组进行了排序,因此您可以进行成对合并,就像合并排序一样,唯一的区别是您尝试比较总和,而不是数字本身。 In other words, say your pointers are currently at A_i and B_j, and you just added A_i + B_j to the list C. Next element might be A_i + B_{j+1} or it might be A_{i+1} + B_j. 换句话说,假设您的指针当前位于A_i和B_j,并且您刚刚将A_i + B_j添加到列表C。下一个元素可能是A_i + B_ {j + 1},也可能是A_ {i + 1} + B_j 。 Simply see which is smaller, and then move that pointer forward. 只需查看哪个较小,然后将指针向前移动即可。

It seems you are already on it, and anothem's answer (or rather the questions!) must have already guided you in the right direction, but if you have more questions, feel free to ask. 看来您已经准备就绪,并且anothem的答案(或更确切地说,是问题!)必须已经在正确的方向上指导了您,但是,如果您还有其他问题,请随时提出。

[PS: No reason to fear duplicates in arrays A, B or C. Duplicates don't change anything for this question. [PS:没有理由担心数组A,B或C中的重复项重复项对此问题没有任何改变。 Consider the data structure to be list, instead of set.] 将数据结构视为列表而不是集合。]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM