My idea is to maintain a k sized max heap…if size of heap becomes > k pop the top element…the top element at any moment will be the kth nearest distance…test cae 1 has passed but test case 2 gives wrong answer…Where am i going wrong ?
code:
#include
#include
using namespace std;
int main() {
priority_queuepq;
int q,k,qt,x,y,dist;
cin>>q>>k;
for(int i=0;i<q;i++)
{
cin>>qt;
if(qt==1){
cin>>x>>y;
dist=(xx)+(yy);
pq.push(dist);
if(pq.size()>k)
pq.pop();
}
if(qt==2)
cout<<pq.top()<<endl;
}
return 0;
}
Hostel Visit Test Case 2
Please post link to the question,
at the point of k+1 insertion .
check
if (distance<pq.top())
{
pq.pop();
pq.push(dist);
}
else
{
//DO NOTHING
}