简体   繁体   English

有没有办法合并两个(或多个)for循环以降低整体时间复杂度?

[英]Is there any way to merge two (or multiple) for loops to reduce the overall time complexity?

Actually I have solved the problem of comparing results of a calculation and returning the minimum value.实际上我已经解决了比较计算结果并返回最小值的问题。 Although the I successfully implemented the logic and executed the code finely but failed to reduce the time complexity.虽然我成功地实现了逻辑并很好地执行了代码,但未能降低时间复杂度。

#include<iostream>
#include<algorithm>
#include<climits>

using namespace std;

int checkMinimum(int array[], int index)
{
  int result = INT_MAX;

  for(int i = 0; i < index; ++i)
    for(int j = 0; j < index; ++j)
      if(i != j && (min(result, ((array[i] & array[j]) ^ (array[i] | array[j]))) < result))
        result = min(result, ((array[i] & array[j]) ^ (array[i] | array[j])));

  return result;
}

void getMinimum()
{
  int index;
  cin >> index;
  int array[index];

  for(int i = 0; i < index; ++i)
    cin >> array[i];

  cout << checkMinimum(array, index) << endl;
}

int main()
{
  int limit;
  cin >> limit;
  while(limit--)
    getMinimum();

  return 0;
}

After researching on for loops and trying to merge them together can't help at all because it's hard to fulfil the conditions of the loops into one.在研究了 for 循环并尝试将它们合并在一起之后根本无济于事,因为很难将循环的条件满足为一个。

#include<iostream>
#include<algorithm>
#include<climits>

using namespace std;

int checkMinimum(int array[], int index)
{
  int result = INT_MAX;

  for(int i = 0, j = index - 1; i != j; ++i, --j)
    if((min(result, ((array[i] & array[j]) ^ (array[i] | array[j]))) < result))
      result = min(result, ((array[i] & array[j]) ^ (array[i] | array[j])));

  return result;
}

void getMinimum()
{
  int index;
  cin >> index;
  int array[index];

  for(int i = 0; i < index; ++i)
    cin >> array[i];

  cout << checkMinimum(array, index) << endl;
}

int main()
{
  int limit;
  cin >> limit;
  while(limit--)
    getMinimum();

  return 0;
}

So how can I reduce the time complexity of this?那么我怎样才能降低这个的时间复杂度呢?

(array[i] & array[j]) ^ (array[i] | array[j]) is equivalent to array[i] ^ array[j] ( & , ^ , | are all bitwise operations; write the truth table for one bit). (array[i] & array[j]) ^ (array[i] | array[j])等价于array[i] ^ array[j] ( & , ^ , |都是按位运算;写出真值表一点点)。

To minimize the XOR, the most important thing is that the high-order bits agree.为了最小化异或,最重要的是高位一致。 Let's say the input has several numbers 0xxxx and several numbers 1xxxx .假设输入有几个数字0xxxx和几个数字1xxxx There is no point in trying the pairs with a 0xxxx and a 1xxxx ;尝试使用0xxxx1xxxx的配对是没有意义的; even if the xxxx part is exactly the same, the XOR is 10000 , whereas the XOR of any 0xxxx with any 0xxxx , or the XOR of any 1xxxx with any 1xxxx is at most 01111 , which is less than 10000 .即使xxxx部分完全相同,异或也是10000 ,而任何0xxxx与任何0xxxx的异或,或任何1xxxx与任何1xxxx的异或最多为01111 ,小于10000

Repeating this logic within each group at the next bit, and the one after that, it's possible to show that the pair with the minimum XOR appear next to each other in sorted order.在每个组中的下一位重复此逻辑,之后的下一位,可以显示具有最小 XOR 的对以排序顺序彼此相邻出现。 Therefore you can sort the array and compute the minimum over n − 1 adjacent pairs.因此,您可以对数组进行排序并计算n − 1相邻对的最小值。

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

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