【问题标题】:Performance of `std::vector` vs `std::queue` [closed]`std::vector` 与 `std::queue` 的性能 [关闭]
【发布时间】:2014-05-31 13:46:58
【问题描述】:

我已向this problem on spoj.com 提交了解决方案,该站点提出了问题,您可以提交解决方案,然后对这些解决方案进行判断并给出三个结果之一 - 接受、错误答案或超过时间限制。我正在处理的特定问题的时间限制为 4 秒。

我的第一个解决方案使用std::vector,如下:

#include<bits/stdc++.h>
using namespace std;
#define inf 10000000
typedef pair<int,int> pii;

int main()
{
int t,n,m;
cin>>t;
char str[200][200];
int dist[200][200];
while(t--)
    {
    cin>>n>>m;
    for(int i=0;i<n;i++)
        scanf("%s",str[i]);

    set< pii > ms;
    for(int i=0;i<n;i++)
        {
        for(int j=0;j<m;j++)
            {
            if(str[i][j]=='1')
                { dist[i+1][j+1]=0;   ms.insert( make_pair<int,int> (i+1,j+1));  }
            else
                dist[i+1][j+1]=inf;
            }
        }
    int dis=0,rr,cc,p,q;
    pii pi,temp;
    pii nei[5];

    while(ms.empty()==0)   // bfs
        {
            pi=*ms.begin();
            p=pi.first;
            q=pi.second;
            ms.erase(ms.begin());

            vector< pii > v;
            v.push_back(make_pair<int,int>(p,q));

                while(!v.empty())
                {
                    temp = *v.begin();
                    v.erase(v.begin());

                    nei[1].first=temp.first-1; nei[1].second=temp.second;
                    nei[2].first=temp.first+1; nei[2].second=temp.second;
                    nei[3].first=temp.first;   nei[3].second=temp.second+1;
                    nei[4].first=temp.first;   nei[4].second=temp.second-1;

                    for(int i = 1; i<=4; ++i)
                    {
                        rr=nei[i].first;
                        cc=nei[i].second;

                        if(rr>=1 && rr<=n && cc>=1 && cc<=m )
                            {
                                dis=abs(rr-p) + abs(cc-q);

                                if(dist[rr][cc] >dis)
                                    {
                                    dist[rr][cc]=dis;
                                    v.push_back(make_pair<int,int>(rr,cc));
                                    }
                            }
                    }
                }
        }

for(int i=1;i<=n;i++)
    {
    for(int j=1;j<=m;j++)
        {
        printf("%d ",dist[i][j]);
        }
    printf("\n");
    }

}   // end of test case

}

当我提交这个时,我的结果是“超出时间限制”。但是,在 ideone it gives the correct result in 0s.

我的第二个解决方案使用std::queue:

#include<bits/stdc++.h>
using namespace std;
#define inf 10000000
typedef pair<int,int> pii;

int main()
{
int t,n,m;
cin>>t;
char str[200][200];
int dist[200][200];
while(t--)
    {
    cin>>n>>m;
    for(int i=0;i<n;i++) 
        scanf("%s",str[i]);

    set< pii > ms;
    for(int i=0;i<n;i++)
        {
        for(int j=0;j<m;j++)
            {
            if(str[i][j]=='1') 
                { dist[i+1][j+1]=0;   ms.insert( make_pair<int,int> (i+1,j+1));  }
            else 
                dist[i+1][j+1]=inf;
            }
        }
    int dis=0,rr,cc,p,q;
    pii pi,temp;
    pii nei[5];

    while(ms.empty()==0)   // bfs 
        {
            pi=*ms.begin();
            p=pi.first;
            q=pi.second;
            ms.erase(ms.begin());

            queue< pii > v;
            v.push(make_pair<int,int>(p,q));

                while(!v.empty())
                {
                    temp = v.front();
                    v.pop();

                    nei[1].first=temp.first-1; nei[1].second=temp.second;
                    nei[2].first=temp.first+1; nei[2].second=temp.second;
                    nei[3].first=temp.first;   nei[3].second=temp.second+1;
                    nei[4].first=temp.first;   nei[4].second=temp.second-1;

                    for(int i = 1; i<=4; ++i)
                    {
                        rr=nei[i].first;  
                        cc=nei[i].second;

                        if(rr>=1 && rr<=n && cc>=1 && cc<=m )
                            {
                                dis=abs(rr-p) + abs(cc-q);

                                if(dist[rr][cc] >dis)
                                    {
                                    dist[rr][cc]=dis;
                                    v.push(make_pair<int,int>(rr,cc));
                                    }
                            }
                    }
                }
        }

for(int i=1;i<=n;i++)
    {
    for(int j=1;j<=m;j++)
        {
        printf("%d ",dist[i][j]);
        }
    printf("\n");
    }

}   // end of test case

}

这个解决方案也是 produces the correct answer in 0s on ideone 并被 spoj.com 接受。

在这两种情况下,容器就像一个“标准队列数据结构”,无论是实现为std::vector 还是std::queue。谁能解释为什么第一个可能因为太慢而被拒绝而第二个被接受?

【问题讨论】:

  • 请正确解释您的问题。我必须阅读您提供的第一个链接上的网站规则,然后按照一系列 URL 运行您的 ideone 示例,然后在我理解之前重新阅读您的问题。相反,将所有相关详细信息和代码示例直接发布到问题中。
  • 我无法发布超过 2 个网址(由于信誉低),因此我不得不使用一系列链接..
  • 正如我已经说过的,将详细信息直接发布到问题中。 Stackoverflow 有专门用于发布代码的标记,您应该在问题中描述 spoj.com 如何工作的相关部分,并仅提供一个链接作为参考点。使用 ideone 例如确定一段代码编译成功,而不是作为代码的存储位置。一个问题应该独立存在,而不是依靠阅读第三方网站来完成它。 URL 很容易被破坏,使问题对未来的读者毫无用处。
  • 我编辑了您的帖子,让您了解如何更好地提出问题以供将来参考。但是请记住,这个问题对于 Stackoverflow 来说可能仍然是题外话,可能更适合 Code Review。

标签: c++ vector stl queue


【解决方案1】:

如果需要队列,请使用队列。

在您使用向量的实现中,您在每次迭代时手动擦除向量的第一个元素,这是对向量的昂贵操作(导致可能重新分配整个缓冲区),而不是在队列上(专门设计对于这种类型的操作_前后修改)。

这解释了为什么基于向量的实现速度较慢。

【讨论】:

    猜你喜欢
    • 2012-06-30
    • 1970-01-01
    • 2010-09-19
    • 1970-01-01
    • 2018-06-06
    • 1970-01-01
    • 2016-12-06
    • 1970-01-01
    • 2020-09-22
    相关资源
    最近更新 更多