简体   繁体   English

无法在大小为1000000000的堆上初始化数组

[英]unable to initialize a array on heap of size 1000000000

I am new to dynamic programming ,I was learning about recursion by solving this question 我是动态编程的新手,我通过解决此问题来学习递归

My Doubt 我的怀疑

1) Since test case is very large How to decide the size of dp table , according to my knowledge we decide the size of table by the value of n; 1)由于测试用例非常大,如何确定dp表的大小,据我所知,我们通过n的值确定表的大小; Please let me know what should correct table size ,because we don,t know how many sub problems going to repeat , i am little confused ; 请让我知道正确的表大小,因为我们不知道要重复多少子问题,我有点困惑;

2) As we know vector dynamically allocates memory on heap and declaring globally a vector mean only pointer variable dp will occupy space in global section , 1000000000* sizeof(int) will be occupied in heap.Then why it showing error std::bad alloc Because we can allocate memory as much as we want on heap 2)正如我们所知,向量在堆上动态分配内存并在全局上声明一个向量意味着仅指针变量dp将在全局部分中占用空间, 1000000000* sizeof(int)将在堆中被占用,然后为什么显示错误std::bad alloc因为我们可以在堆上分配尽可能多的内存

    #include<bits/stdc++.h>
    using namespace std;
    vector <int> dp(1000000001,-1);
    int exchange(int n){
        if(n<12)
            return n;
        if(dp[n]!=-1)
            return dp[n];
        return dp[n] = exchange(n/2)+exchange(n/3)+exchange(n/4);
    }
    int main(){
        int t;
        cin>>t;
        while(t--){
      //      memset(dp,-1,sizeof(dp));
            int n;
            cin>>n;
            cout<<exchange(n);
        }

    }

EDIT 编辑

Note-> count() The function returns the number of times the key K is present in the map container. 注意-> count()函数返回键K在地图容器中的出现次数。 It returns 1 if the key is present in the container as the map only contains a unique key. 如果密钥存在于容器中,则返回1,因为映射仅包含唯一密钥。 It returns 0 if the key is not present in the map container. 如果键在地图容器中不存在,则返回0。 It is passing nearly all the test cases but it is failing 1 000 000 000 according to long long int range value it should pass this one also but it giving some negative value as output ; 它通过了几乎所有的测试用例,但根据long long int范围值却失败了1 000 000 000 ,它也应该通过这一测试,但是它给出了一些负值作为输出; I think this is the problem with container map 我认为这是容器映射的问题

    #include<bits/stdc++.h>
    using namespace std;
    map <long long int,long long int> dp;
    int exchange(long long int n){
        if(n<12)
            return n;
        if(dp.count(n))
            return dp[n];
        return dp[n] = exchange(n/2)+exchange(n/3)+exchange(n/4);
    }
    int main(){
     //   int t;
       // cin>>t;
        while(1){
      //      memset(dp,-1,sizeof(dp));
            long long int n;
            cin>>n;
            cout<<exchange(n)<<endl;
        }

    }

You can avoid the immediate problem by doing: 您可以通过执行以下操作避免出现直接问题:

vector <int> dp;

// ....

cin>>t;
dp = std::vector<int>(t, -1);

This will dynamically allocate the memory based on the entered t , and you can use exceptions to catch if the user enters too large a value. 这将根据输入的t动态分配内存,如果用户输入的值太大,则可以使用异常来捕获。

While your input size may be very large, note that the vast majority of your table will not be inspected. 尽管您的输入大小可能非常大,但是请注意,将不会检查表的绝大多数。 For an input of 999,999,999 you will only ever touch 230 values of exchange(n) . 对于999,999,999的输入,您将仅触摸230的exchange(n)值。 Roughly speaking, you will descend log_2(n) times with a branching factor of 2 (since the n/4 case is covered by n/2 applied twice). 粗略地讲,您将以2的分支因子下降log_2(n)次(因为n / 4情况被两次应用的n / 2覆盖)。

For this concrete example, you're thus better off using a map<int, int> or an unordered_map<int, int> . 对于此具体示例,因此最好使用map<int, int>unordered_map<int, int>

Your concrete exchange function might look like the following: 您的具体exchange函数可能如下所示:

std::map<int, int> dp;

int exchange(int n) {
    if (n < 12) return n;
    auto it = dp.find(n);
    if (it != dp.cend()) return it->second;

    int val = exchange(n/2) + exchange(n/3) + exchange(n/4);
    dp.insert({n, val});
    return val;
}

Vector allocates ONE block of memory, so it is possible that it doesn't found free memory block(read about memory fragmentation). Vector分配一个内存块,因此有可能找不到空闲的内存块(有关内存碎片的信息)。

What's more , there is possibility of using max 2GB per one process on 32bit systems and 8TB on 64bit systems, of course if your machine has so much memory. 什么是更多 ,对64个系统的32位系统和8TB使用最大2GB的每一个过程,当然如果你的机器有那么多内存的可能性。

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

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