Hostel visits problem

Can you please tell me what is wrong with my code.

Hey @rahul.gupta03111999

			while(k!=(i+1)){//updated this cond
				a[i]=pq.top();
				i++;
				pq.pop();
			}
			cout<<pq.top()<<endl;
			if(k>1){
			   for(int j=k-2;j>=0;j--){
                pq.push(a[j]);//pq.push(a[j]) here instead of a[i]
			   }
			}

Here update this 2 places and your code is good to go :slight_smile:
If this resolves your query then please mark it as resolved :slight_smile:

You will still suffer TLE ,for that mantain your heap of size K at max

The following problem can be easily solved by using heaps.
For every query of type 1, insert elements until the size of the heap becomes ‘k’.
Then for every query of type 1 after reaching the size k for heap(max-heap) we will check if the current element is smaller than the root of the heap or not. If it is not smaller then we ignore it else we remove the root of the heap and push the new element in the heap. (What this will do is maintain a heap of size k which will contain k nearest coordinates for the dean) .
For every query of type 2 just print the root of the heap.

I have made the changes that you told, but only one test case is passing. Please Help

will check in half n hr :slight_smile:

1 Like

Hey @rahul.gupta03111999
Make x,y long long instead of int

If this resolves your query then please mark it as resolved :slight_smile:

@Kartikkhariwal1 here the range for x and y is: -10^6 to 10^6, then why to take long long instead of int. as far my knowledge, we should take long long for the number greater than 10^9, right!
Make me correct if I am wrong.

btw could successfully submit after taking long long instead of int. Just want to know the reason for taking long long instead int.

Thanks!

Hey @swastikswarupmeher
Its because later you will do xx+yy which will overflow for int ,hence making x and y long long will make sure that xx+yy does not overflow.

If this resolves your query then please mark it as resolved :slight_smile:

@Kartikkhariwal1 yehh got it thanks!

1 Like