首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >数组中3个长度AP的最快算法计数

数组中3个长度AP的最快算法计数
EN

Stack Overflow用户
提问于 2012-11-06 05:03:06
回答 2查看 913关注 0票数 1

我想解决this CodeChef挑战:

假设我们有一个由N个(范围100,000)个元素组成的数组A。我们将求出所有3对这样的元素1<=Ai,Aj,Ak<=30,000的计数,使得

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

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

代码语言:javascript
复制
9 4 2 3 6 10 3 3 10

因此,AP是:

代码语言:javascript
复制
{2,6,10},{9,6,3},{9,6,3},{3,3,3},{2,6,10} 

因此,必填答案是5。

我的方法

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

如果我们在第i个位置,则在i之前存储数组值的计数,而right在i之后存储数组的计数。i简单地循环查找数组中所有可能的公差。代码如下:

代码语言:javascript
复制
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]];
}

但这会超出我的时间限制。请帮我找出更好的算法。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-11-06 05:10:55

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

伪C++-y代码:

代码语言:javascript
复制
// 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;
票数 3
EN

Stack Overflow用户

发布于 2012-11-06 07:01:14

这是一个简单的C语言版本的解决方案,它利用了Ai + Ak的优势,必须进行偶数测试:

代码语言:javascript
复制
#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);
}

和控制台运球:

代码语言:javascript
复制
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平方(类似于)。

代码语言:javascript
复制
#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);
}

还有运球:

代码语言:javascript
复制
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$ 
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13240330

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档