[英]C++ vector and memoization runtime error issues
I encountered a problem here at Codechef. 我遇到一个问题在这里的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. 由于我仍然是编程新手,并且对STL容器不熟悉,因此我将vector
用于查找表。 (although, I was suggested that using map
helps to solve the problem). (尽管有人建议我使用map
来解决问题)。
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. 为了得到错误,我使用问题的边界值( 100000000
)作为输入。 The error message displayed by my Netbeans IDE is RUN FAILED (exit value 1, total time: 4s)
with input as 1000000000
. 我的Netbeans IDE显示的错误消息是RUN FAILED (exit value 1, total time: 4s)
,输入为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;
}
I doubt if this is a memory issue because he already said that the program actually runs and he inputs 100000000. 我怀疑这是否是内存问题,因为他已经说过该程序实际上在运行,并且他输入了100000000。
One things that I noticed, in the if condition you're doing a lookup[n] even if n == MAXSIZE (in this exact condition). 我注意到的一件事,在if条件下,即使n == MAXSIZE(在此精确条件下),您也要执行lookup [n]。 Since C++ is uses 0-indexed vectors, then this would be 1 beyond the end of the vector. 由于C ++使用索引为0的向量,因此该向量将比向量的末尾多1。
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. 我猜不出算法在做什么,但是我认为第一个“ if”的右括号应降低,并且您可能会在此边界条件下返回错误。
您要么没有足够的内存,要么没有足够的连续地址空间来存储100,000,000个unsigned long
s。
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. 对于您的情况,使用8字节的双字节,您基本上是在要求您的计算机在单个块中提供大约762 mb的内存。
I don't know which problem you're solving, but it looks like you're solving Bytelandian coins. 我不知道您要解决哪个问题,但看起来您正在解决Bytelandian硬币。 For this, it is much better to use a map, because: 为此,最好使用地图,因为:
So, the best thing to use in your situation is an std::map. 因此,在您的情况下最好使用的是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]. 在您的情况下,实际上只需将std::vector<unsigned long>
替换为std::map<int, unsigned long>
,因为map
也具有[]
操作员访问权限[在大多数情况下,应该如此]。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.