-3

question: Dean of MAIT is going to visit Hostels of MAIT. As you know that he is a very busy person so he decided to visit only the first "K" nearest Hostels. Hostels are situated on a 2D plane. You are given the coordinates of hostels and you have to answer the Rocket distance of Kth nearest hostel from the origin ( Dean's place )

Input Format The first line of input contains Q Total no. of queries and K There are two types of queries:

first type: 1 x y For query of 1st type, you came to know about the coordinates ( x, y ) of the newly constructed hostel. second type: 2 For query of 2nd type, you have to output the Rocket distance of Kth nearest hostel till now.

//The Dean will always stay at his place ( origin ). It is guaranteed that there will be at least k queries of type 1 before the first query of type 2.

Rocket distance between two points ( x2 , y2 ) and ( x1 , y1 ) is defined as (x2 - x1)2 + (y2 - y1)2

Constraints 1 < = k < = Q < = 10^5 -10^6 < = x , y < = 10^6

Output Format For each query of type 2 output the Rocket distance of Kth nearest hostel from Origin.//

This is my code:

#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;

}

Original link to code: https://codeshare.io/2j1bkA

What's wrong with my code?

Lukas-T
  • 11,133
  • 3
  • 20
  • 30
  • 2
    Welcome to Stack Overflow. Please read [the help pages](http://stackoverflow.com/help), take [the SO tour](http://stackoverflow.com/tour), read about [how to ask good questions](http://stackoverflow.com/help/how-to-ask), as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). Lastly please learn how to create a [mcve] to show us *in the question itself*. – Some programmer dude Feb 06 '20 at 07:12
  • 2
    Why do you think that there's something wrong with your code – Thomas Sablik Feb 06 '20 at 07:51
  • I added the code from the link. In the future please don't link to your code, just copy-paste it in the question. Or if you think you have too much code, create a [MRE](https://stackoverflow.com/help/minimal-reproducible-example) instead. And please also tell what is wrong with your code. Does it compile? Does it crash at runtime? Does it produce wrong results? – Lukas-T Feb 06 '20 at 08:25

1 Answers1

0

Please consider adding what problem you are facing in your post. I've seen your code but I couldn't help with your code as I even saw a syntax error in your code and didn't know if it was the problem.

If I didn't misunderstand your question, std::set will fit better than priority queue as it is not possible to remove the largest item in a priority queue(min heap). The following code should work:

#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;
}
TYeung
  • 2,579
  • 2
  • 15
  • 30
  • the complexity of building a heap is o(n) time and the complexity of extracting top k elements is klogn so the total complexity of the solution is klogn time which is efficient i guess .my code has no compilation errors ,it is just that it is not passing the test cases ,i tried your code running but it has many compilation errors .but thanks for looking after my problem. – Ritik gupta Feb 07 '20 at 07:33
  • The line after int main() is already a compilation error, maybe you just make a typo there – TYeung Feb 07 '20 at 10:07
  • I edited the post, I thought the compilation error is caused by the line (distances.end() - 1). I didn't notice it as I didn't run my code. Multi-set iterator is bidirectional so I can't do -1. Aside from this, priority queue is also viable for this question. It works basically the same as a set in this problem. – TYeung Feb 07 '20 at 10:32
  • If you want me to debug your code, fix compilation error and describe briefly what you are trying to do, as I saw lots of redundant code. – TYeung Feb 07 '20 at 10:34