C++矢量和记忆运行时错误问题

发布于 2024-12-29 06:57:18 字数 1261 浏览 0 评论 0原文

我在 Codechef 此处遇到了问题。我正在尝试使用向量进行记忆。由于我对编程还是个新手,而且对 STL 容器也不太熟悉,所以我使用了 vector 作为查找表。 (尽管,有人建议我使用 map 有助于解决问题)。

所以,我的问题是下面给出的解决方案如何遇到运行时错误。为了得到错误,我使用问题的边界值 (100000000) 作为输入。我的 Netbeans IDE 显示的错误消息是 RUN FAILED (exit value 1, Total time: 4s),输入为 1000000000。这是代码:

#include <iostream>
#include <cstdlib>
#include <vector>
#include <string>

#define LCM 12
#define MAXSIZE 100000000
using namespace std;
/*
 * 
 */
vector<unsigned long> lookup(MAXSIZE,0);

int solve(int n)
{
    if ( n < 12) {
        return n;
    }
    else {
        if (n < MAXSIZE)  {
            if (lookup[n] != 0) {
                return lookup[n];
            }
        }

            int temp = solve(n/2)+solve(n/3)+solve(n/4);
            if (temp >= lookup[n] ) {
                lookup[n] = temp;
            }
            return lookup[n];

    }
}
int main(int argc, char** argv) {
    int t;
    cin>>t;
    int n;
    n = solve(t);
    if ( t >= n) {
        cout<<t<<endl;
    }
    else {
        cout<<n<<endl;
    }
    return 0;
}

I encountered a problem here at Codechef. I am trying to use a vector for memoization. As I am still new at programming and quite unfamiliar with STL containers, I have used vector, for the lookup table. (although, I was suggested that using map helps to solve the problem).

So, my question is how is the solution given below running into a run time error. In order to get the error, I used the boundary value for the problem (100000000) as the input. The error message displayed by my Netbeans IDE is RUN FAILED (exit value 1, total time: 4s) with input as 1000000000. Here is the code:

#include <iostream>
#include <cstdlib>
#include <vector>
#include <string>

#define LCM 12
#define MAXSIZE 100000000
using namespace std;
/*
 * 
 */
vector<unsigned long> lookup(MAXSIZE,0);

int solve(int n)
{
    if ( n < 12) {
        return n;
    }
    else {
        if (n < MAXSIZE)  {
            if (lookup[n] != 0) {
                return lookup[n];
            }
        }

            int temp = solve(n/2)+solve(n/3)+solve(n/4);
            if (temp >= lookup[n] ) {
                lookup[n] = temp;
            }
            return lookup[n];

    }
}
int main(int argc, char** argv) {
    int t;
    cin>>t;
    int n;
    n = solve(t);
    if ( t >= n) {
        cout<<t<<endl;
    }
    else {
        cout<<n<<endl;
    }
    return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

故事未完 2025-01-05 06:57:18

我怀疑这是否是一个内存问题,因为他已经说过该程序实际上运行并且他输入了 100000000。

我注意到的一件事是,在 if 条件下,即使 n == MAXSIZE (在此确切的条件)。由于 C++ 使用 0 索引向量,因此超出向量末尾的值将是 1。

    if (n < MAXSIZE)  {
     ...
    }

        ...
        if (temp >= lookup[n] ) {
            lookup[n] = temp;
        }
        return lookup[n];

我无法猜测该算法正在做什么,但我认为第一个“if”的右大括号 } 应该较低,并且您可能会在此边界条件上返回错误。

I doubt if this is a memory issue because he already said that the program actually runs and he inputs 100000000.

One things that I noticed, in the if condition you're doing a lookup[n] even if n == MAXSIZE (in this exact condition). Since C++ is uses 0-indexed vectors, then this would be 1 beyond the end of the vector.

    if (n < MAXSIZE)  {
     ...
    }

        ...
        if (temp >= lookup[n] ) {
            lookup[n] = temp;
        }
        return lookup[n];

I can't guess what the algorithm is doing but I think the closing brace } of the first "if" should be lower down and you could return an error on this boundary condition.

隐诗 2025-01-05 06:57:18

您要么没有足够的内存,要么没有足够的连续地址空间来存储 100,000,000 个 unsigned long

You either don't have enough memory or don't have enough contiguous address space to store 100,000,000 unsigned longs.

潦草背影 2025-01-05 06:57:18

这主要是内存问题。对于向量,您需要连续的内存分配[以便它能够跟上其恒定时间查找的承诺]。在你的例子中,使用 8 字节双精度,你基本上是在请求你的机器在一个块中为你提供大约 762 mb 的内存。

我不知道你正在解决哪个问题,但看起来你正在解决 Bytelandian 硬币。为此,最好使用映射,因为:

  1. 您通常不会在测试用例运行中存储所有 100000000 个用例的值。因此,您需要一种只为您实际记忆的值分配内存的方法。
  2. 即使是,您也不需要持续时间查找。尽管 std::map 会加快程序速度,但它使用树来为您提供对数查找时间。它消除了连续使用 762 mb 的要求。 762 mb 不是什么大问题,但在单个块中的预期却是大问题。

因此,在您的情况下最好使用的是 std::map。在您的情况下,实际上只需将 std::vector 替换为 std::map 就可以作为 map 还具有 [] 操作员访问权限 [在大多数情况下,它应该]。

This mostly is a memory issue. For a vector, you need contiguous memory allocation [so that it can keep up with its promise of constant time lookup]. In your case, with an 8 byte double, you are basically requesting your machine to give you around 762 mb of memory, in a single block.

I don't know which problem you're solving, but it looks like you're solving Bytelandian coins. For this, it is much better to use a map, because:

  1. You will mostly not be storing the values for all 100000000 cases in a test case run. So, what you need is a way to allocate memory for only those values that you are actually memoize.
  2. Even if you are, you have no need for a constant time lookup. Although it would speed up your program, std::map uses trees to give you logarithmic look up time. And it does away with the requirement of using up 762 mb contiguously. 762 mb is not a big deal, but expecting in a single block is.

So, the best thing to use in your situation is an std::map. In your case, actually just replacing std::vector<unsigned long> by std::map<int, unsigned long> would work as map also has [] operator access [for the most part, it should].

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文