【问题标题】:hostel visit question (priority queue application )宿舍访问问题(优先队列申请)
【发布时间】:2020-05-22 04:09:07
【问题描述】:

问题:MAIT的院长要去参观MAIT的宿舍。如您所知,他是一个非常忙碌的人,因此他决定只访问第一个“K”最近的旅馆。旅馆位于二维平面上。你得到了宿舍的坐标,你必须回答距离原点(Dean's place)最近的第 K 个宿舍的火箭距离

输入格式 输入的第一行包含 Q Total no。的查询和 K 有两种查询:

第一种类型:1 x y 对于第一种类型的查询,您了解了新建宿舍的坐标(x,y)。 second type: 2 查询2nd type,需要输出Kth最近旅馆的Rocket距离到现在。

//院长将永远留在他的地方(原点)。保证在第一个类型 2 查询之前至少有 k 个类型 1 查询。

两点(x2,y2)和(x1,y1)之间的火箭距离定义为(x2 - x1)2 + (y2 - y1)2

约束 1

输出格式 对于类型 2 的每个查询,输出距离 Origin 的第 K 个最近旅馆的 Rocket 距离。//

这是我的代码:

#include <iostream>
#include <queue>
#include <vector>
#include <algorithm>

using namespace std;

class roomno
{
    public:
    int x;
    int y;

    roomno(int x,int y)
    {
        this->x=x;
        this->y=y;
    }
    void print()
    {
        cout<<"location"<<"("<<x<<","<<y<<")"<<endl;
    }

  int distance ()
  {
      return (x*x+y*y);
  }

};

class roomcompare
{
    public:
bool operator() (roomno r1,roomno r2)
{
    return r1.distance()>r2.distance();
}
};


int main() 
{   
    int x[1000]},y[1000];
    int l,k=0;

   priority_queue<roomno,vector<roomno>,roomcompare> pq;
     int n,i,j;
   cin>>n>>l;
     //cin>>n;
     cin.ignore();
    for( i=0;i<n;i++)
    {
        cin>>x[i];
    }
    cin.ignore();
    for( j=0;j<n;j++)
    {
        cin>>y[j];
    }
    cin.ignore();

    for(i=0;i<n;i++ )
    {
        roomno r1(x[i],y[i]);
        pq.push(r1);
    }
    while(!pq.empty()&&k!=l)
    {   k++;
        roomno r2=pq.top();
        r2.print();
        pq.pop();

    }
    return 0;

}

原始代码链接:https://codeshare.io/2j1bkA

我的代码有什么问题?

【问题讨论】:

  • 欢迎来到 Stack Overflow。请阅读the help pages,获取the SO tour,了解how to ask good questions,以及this question checklist。最后,请学习如何创建minimal reproducible example 以向我们展示问题本身
  • 为什么你认为你的代码有问题
  • 我从链接中添加了代码。以后请不要链接到您的代码,只需将其复制粘贴到问题中即可。或者,如果您认为自己的代码过多,请创建一个 MRE 代替。并且请告诉您的代码有什么问题。它编译吗?它会在运行时崩溃吗?它会产生错误的结果吗?

标签: c++ min-heap


【解决方案1】:

请考虑在帖子中添加您面临的问题。我看过你的代码,但我对你的代码无能为力,因为我什至在你的代码中看到了语法错误,不知道是不是问题所在。

如果我没有误解您的问题,std::set 将比优先队列更适合,因为无法删除优先队列(最小堆)中的最大项目。以下代码应该可以工作:

#include <iostream>
#include <set>
using namespace std;
using ll = long long;
multiset<ll> distances;
int main() {
    ll n, k;
    cin >> n >> k;
    for(ll i = 0; i < n; ++i) {
        ll query; 
        cin >> query;
        if (query == 1) {
            ll x, y;
            cin >> x >> y;
            distances.insert(x * x + y * y);
            if (distances.size() > k) {
                distances.erase(--distances.end());
            }
        } else {
            cout << *distances.rbegin() << '\n';
        }
    }
    return 0;
}

【讨论】:

  • 构建堆的复杂度是 o(n) 时间,提取前 k 个元素的复杂度是 klogn,所以解决方案的总复杂度是 klogn 时间,我猜这是有效的。我的代码没有编译错误,只是它没有通过测试用例,我尝试运行你的代码,但它有很多编译错误。但是感谢您照顾我的问题。
  • int main()后面的那行已经是编译错误了,可能是你打错字了
  • 我编辑了帖子,我认为编译错误是由 (distances.end() - 1) 行引起的。我没有注意到它,因为我没有运行我的代码。多集迭代器是双向的,所以我不能做-1。除此之外,优先队列对于这个问题也是可行的。它的工作原理与本题中的一组基本相同。
  • 如果你想让我调试你的代码,修复编译错误并简要描述你想要做什么,因为我看到了很多冗余代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-09
  • 1970-01-01
  • 2021-11-18
  • 2020-12-04
  • 2015-03-12
  • 1970-01-01
相关资源
最近更新 更多