简体   繁体   English

在C ++中,映射比堆有什么优势

[英]what is the advantage of map over heap in c++

currently i am doing problem on data structure and i have a question in which i have to find kth largest element in an array. 目前,我在数据结构上遇到问题,并且我有一个问题,我必须在数组中找到第k个最大元素。 the actual problem is here: https://www.geeksforgeeks.org/kth-smallestlargest-element-unsorted-array/ . 实际的问题在这里: https : //www.geeksforgeeks.org/kth-smallestlargest-element-unsorted-array/

i did this question in two different way using heap and second is using map. 我以两种不同的方式使用堆做了这个问题,第二种是使用地图。

my solution using map. 我的解决方案使用地图。

   int t;
cin>>t;
while(--t>=0){
int n,k;
cin>>n;
vector<int> A(n);
for(int i=0;i<n;i++){
cin>>A[i];
 }
cin>>k;
map<int,int> m;
for(int i=0;i<n;i++){
    m[A[i]]++;
}
auto it=m.begin();
for(int i=1;i<=k-1;i++){
    it++;
}
cout<<it->first<<endl;

but my map solution is giving Time Limit Exceeded. 但我的地图解决方案给出了超过时间限制。 according to me map solution also has a time complexity of (n+klog(n)), same as heap solution. 根据我的说法,地图解决方案的时间复杂度为(n + klog(n)),与堆解决方案相同。 so why is map solution giving TLE? 那么,为什么地图解决方案赋予TLE?

Without seeing your heap version I would guess the map's allocation is the cause of the trouble. 没有看到您的堆版本,我可能会认为映射的分配是造成问题的原因。 Each node needs an allocation, which implies a lock and some additional management. 每个节点都需要分配,这意味着有一个锁和一些其他管理。

And you have to follow the pointers internally in the map data structure, which is usually not the case in heaps. 而且您必须在内部遵循地图数据结构中的指针,而在堆中通常不是这种情况。

In big O notation this doesn't add anything to the time, but in practice each can slow down the program by a large factor. 用大O表示法不会增加任何时间,但实际上每个方法都会使程序运行变慢。

The time complexity for your solution using maps would be O(k + nlog(n)). 使用地图的解决方案的时间复杂度为O(k + nlog(n))。 Each insertion into std::map takes log(n) time and you are performing 'n' insertions. 每次插入std::map都会花费log(n)时间,并且您正在执行'n'插入。 Time complexity for insertion alone would take O(nlog(n)) time. 单独插入的时间复杂度将花费O(nlog(n))时间。

See http://www.cplusplus.com/reference/map/map/insert/ for more information about inserting elements into std::map . 有关将元素插入std::map更多信息,请参见http://www.cplusplus.com/reference/map/map/insert/

A simple improvement to the algorithm is that there is a "trick": * If you only need to print "k" elements, then you only need to remember the "k" biggest elements. 该算法的一个简单改进是存在一个“技巧”:*如果只需要打印“ k”个元素,则只需要记住“ k”个最大元素。 That way the insert operations never exceed O(log k) rather than O(log n). 这样,插入操作就不会超过O(log k)而不是O(log n)。 And k is presumably much less than n... k大概比n小得多

So instead of inserting everything into a map that gets bigger and bigger (and slower and slower - you mention a time-restriction). 因此,与其将所有内容插入到越来越大的地图中(越来越慢-您提到时间限制),不如将其插入地图。 Change the code to remove the smallest element in the map once (map.size() >= k): 更改代码以一次删除地图中的最小元素(map.size()> = k):

    map<int,int> m;
    for(int i = 0; i < n ; i++) {
        m[A[i]]++;
        if(m.size() > k) {           // <<<<<<<<
             m.erase(m.begin())      // <<<<<<<<
        }
    }

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

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