priority queue help needed

i am declaring a pair of priority queue ,and adding nunbers such as
1 2,
2 3 ,
2 3,
3 4.
so in priority queue the sequence will be
3 4 , 2 3 , 2 3, 1 2.
i want to pop out 3 such that , at the end
3 3 , 2 3, 2 3,1 2 are left ,
how will i do these in priority query ?
i hv less idea abt PQ, so any help will be helpful , i hv declared it as priority_queue <pair<ll,ll>> pq;
will pq.top pop out 3? the next question is how to decrement 3’s 3 to 2.

1 Like

You can’t access and change a certain index of a priority_queue like an array or a vector. Only thing you can do is push elements, see what’s in the top and pop elements. So if you want to pop out 3, it has to be in the top and then you can pop it out. Again if you need to decrement 3’s 3 to 2, you need to access the top, pop it out, decrement the value and then push it again. You can implement some if conditions to check these things.

1 Like

so if at the top, the pair has 3 and 4 respectively , i wanna pop out 3 only , and decrement 4 to 3 . how to do that?

you can’t just pop out 3 only, because it’s a pair. You have to pop it out as a pair. You can do this :

pair<int,int> p = pq.top();   //storing the pair that's in the top
pq.pop();  //removing the top element
if(p.first == 3 && p.second == 4) //conditions to decrement
    p.second--;
pq.push(p); //pushing the pair in the priority queue
1 Like

thnks brother , it helped a lot actually

1 Like

welcome. Happy to help! :slight_smile:

you don’t you ask the one who told you to use priority queue?

1 Like