1

我想解决这个CodeChef 挑战:

假设我们有一个包含 N(范围为 100,000)个元素的数组 A。我们要找到所有 3 个这样的元素对的计数 1<=Ai,Aj,Ak<=30,000 使得

Aj-Ai = Ak- Aj and i < j < k

换句话说 Ai,Aj,Ak 在算术级数中。例如对于 Array :

9 4 2 3 6 10 3 3 10


所以AP是:

{2,6,10},{9,6,3},{9,6,3},{3,3,3},{2,6,10} 


所以要求的答案是 5。

我的方法

我尝试的是取 30,000 个名为过去和正确的长数组。最初 right 包含每个 1-30,000 元素的计数。

如果我们在第 i 个位置,则过去存储 i 之前的数组值的计数,而 right 存储 i 之后的数组的计数。我只是循环查找数组中所有可能的共同差异。这是代码:

right[arr[1]]--;

for(i=2;i<=n-1;i++)
{
    past[arr[i-1]]++;
    right[arr[i]]--;
    k=30000 - arr[i];
    if(arr[i] <= 15000)
        k=arr[i];
    for(d=1;d<=k;d++)
    {
        ans+= right[arr[i] + d]*past[arr[i]-d] + past[arr[i] + d]*right[arr[i]-d];
    }
    ans+=past[arr[i]]*right[arr[i]];
}



但这让我超过了时间限制。请帮助更好的算法。

4

2 回答 2

3

如果您对列表进行第一次遍历,并且只提取可能有 3 项 AP 之间的数字对(差异为 0 mod 2),则可以大大缩短执行时间。然后在这些对之间迭代。

伪 C++-y 代码:

// Contains information about each beginning point
struct BeginNode {
  int value;
  size_t offset;
  SortedList<EndNode> ends;  //sorted by EndNode.value
};

// Contains information about each class of end point
struct EndNode {
  int value;
  List<size_t> offsets; // will be sorted without effort due to how we collect offsets
};

struct Result {
  size_t begin;
  size_t middle;
  size_t end;
};

SortedList<BeginNode> nodeList;
foreach (auto i : baseList) {
  BeginNode begin;
  node.value = i;
  node.offset = i's offset; //you'll need to use old school for (i=0;etc;i++) with this
  // baseList is the list between begin and end-2 (inclusive)
  foreach (auto j : restList) { 
    // restList is the list between iterator i+2 and end (inclusive)
    // we do not need to consider i+1, because not enough space for AP
    if ((i-j)%2 == 0) { //if it's possible to have a 3 term AP between these two nodes
      size_t listOffset = binarySearch(begin.ends);
      if (listOffset is valid) {
        begin.ends[listOffset].offsets.push_back(offsets);
      } else {
        EndNode end;
        end.value = j;
        end.offsets.push_back(j's offset);
        begin.ends.sorted_insert(end);
      }
    }
  }
  if (begin has shit in it) {
    nodeList.sorted_insert(begin);
  }
}
// Collection done, now iterate over collection

List<Result> res;
foreach (auto node : nodeList) {
  foreach (auto endNode : node.ends) {
    foreach (value : sublist from node.offset until endNode.offsets.last()) {
      if (value == average(node.value, endNode.value)) {
        // binary_search here to determine how many offsets in "endNode.offsets" "value's offset" is less than.
        do this that many times:
          res.push_back({node.value, value, endNode.value});
      }
    }
  }
}

return res;
于 2012-11-05T21:10:55.013 回答
1

这是一个简单的 C 版本的解决方案,它利用了 Ai + Ak must be even test:

#include <stdio.h>

static int arr[] = {9, 4, 2, 3, 6, 10, 3, 3, 10};

int main ()
{
    int i, j, k;
    int sz = sizeof(arr)/sizeof(arr[0]);
    int count = 0;

    for (i = 0; i < sz - 2; i++)
    {
        for (k = i + 2; k < sz; k++)
        {
            int ik = arr[i] + arr[k];
            int ikdb2 = ik / 2;

            if ((ikdb2 * 2) == ik) // if ik is even
            {
                for (j = i + 1; j < k; j++)
                {
                    if (arr[j] == ikdb2)
                    {
                        count += 1;
                        printf("{%d, %d, %d}\n", arr[i], arr[j], arr[k]);
                    }
                }
            }
        }
    }
    printf("Count is: %d\n", count);
}

和控制台运球:

tmp e$ cc -o triples triples.c
tmp e$ ./triples
{9, 6, 3}
{9, 6, 3}
{2, 6, 10}
{2, 6, 10}
{3, 3, 3}
Count is: 5
tmp e$ 

这个更复杂的版本保留了一个按值索引的 Aj 列表,从 n 立方到 n 平方(有点)。

#include <stdio.h>
#include <stdint.h>

static uint32_t arr[] = {9, 4, 2, 3, 6, 10, 3, 3, 10};

#define MAX_VALUE 100000u
#define MAX_ASIZE  30000u

static uint16_t index[MAX_VALUE+1];
static uint16_t list[MAX_ASIZE+1];

static inline void remove_from_index (int subscript)
{
    list[subscript] = 0u; // it is guaranteed to be the last element

    uint32_t value = arr[subscript];

    if (value <= MAX_VALUE && subscript == index[value])
    {
        index[value] = 0u; // list now empty
    }
}

static inline void add_to_index (int subscript)
{
    uint32_t value = arr[subscript];

    if (value <= MAX_VALUE)
    {
        list[subscript] = index[value]; // cons
        index[value] = subscript;
    }
}

int main ()
{
    int i, k;
    int sz = sizeof(arr)/sizeof(arr[0]);
    int count = 0;

    for (i = 0; i < sz - 2; i++)
    {
        for (k = i; k < sz; k++) remove_from_index(k);

        for (k = i + 2; k < sz; k++)
        {
            uint32_t ik = arr[i] + arr[k];
            uint32_t ikdb2 = ik / 2;

            add_to_index(k-1); // A(k-1) is now a legal middle value 

            if ((ikdb2 * 2) == ik) // if ik is even
            {
                uint16_t rover = index[ikdb2];

                while (rover != 0u)
                {
                    count += 1;
                    printf("{%d, %d, %d}\n", arr[i], arr[rover], arr[k]);

                    rover = list[rover];
                }
            }
        }
    }
    printf("Count is: %d\n", count);
}

和运球:

tmp e$ cc -o triples triples.c
tmp e$ ./triples
{9, 6, 3}
{9, 6, 3}
{2, 6, 10}
{2, 6, 10}
{3, 3, 3}
Count is: 5
tmp e$ 
于 2012-11-05T23:01:13.820 回答