Hostel visit problem

Sir i applied 2 approaches in this question hostel visit.
First one I used priority queues but in one of the test cases it is giving TLE.
Second approach I used is policy based data struture and in it also one of the test cases is giving wrong ans.

Can you please suggest me what is wrong in my code for both approaches.

@satyansh1399 in priority queue you are using min heap when you should be using max heap. Make sure you are using long long int to store the values.

#include<bits/stdc++.h> #define ll long long int using namespace std; int main() { ll q,k; cin>>q>>k; priority_queue<ll,vector,greater >pq1,pq2; for(ll i=0;i<q;i++){ ll query,x,y; cin>>query; if(query==1){ cin>>x>>y; ll solve=xx+yy; pq1.push(solve); } else{ pq2=pq1; ll counter=1; while(counter<=k){ if(counter==k){ cout<<pq2.top()<<endl; break; } counter++; pq2.pop(); } } } return 0; }

yes i a using the long long int to store it

@satyansh1399 use MAX HEAP.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.

I am still getting TLE. Now i have used max heap.

share your code in ide.codingblocks.com
I may be able to debug!!

(#note: if you reply to this message please mention “@rkrishna” (without quotation marks " ", so that I will get notification)

@satyansh1399 Your doubt was reopened.
Is your doubt resolved?

@rkrishna #include<bits/stdc++.h> #define ll long long int using namespace std; int main() { ll q,k; cin>>q>>k; priority_queuepq1,pq2; for(ll i=0;i<q;i++){ ll query,x,y; cin>>query; if(query==1){ cin>>x>>y; ll solve=xx+yy; pq1.push(solve); } else{ pq2=pq1; ll counter=pq1.size()-k; while(counter){ counter–; pq2.pop(); } cout<<pq2.top()<<endl; } } return 0; }

@satyansh1399 please share your code by saving it on cb ide

@rkrishna https://ide.codingblocks.com/s/281750

@satyansh1399 you dont need 2 priority queues, make the changes in one pq. See this for clarification on the logic.

1 Like

Thanks mam i got it correct

1 Like