I want to delete some element in a priority queue not at the top, how should i do? can someone suggest any method, in constant or logarithmic time. How to handle duplicates while removing
# | User | Rating |
---|---|---|
1 | tourist | 4009 |
2 | jiangly | 3831 |
3 | Radewoosh | 3646 |
4 | jqdai0815 | 3620 |
4 | Benq | 3620 |
6 | orzdevinwang | 3529 |
7 | ecnerwala | 3446 |
8 | Um_nik | 3396 |
9 | gamegame | 3386 |
10 | ksun48 | 3373 |
# | User | Contrib. |
---|---|---|
1 | cry | 164 |
1 | maomao90 | 164 |
3 | Um_nik | 163 |
4 | atcoder_official | 160 |
5 | -is-this-fft- | 158 |
6 | awoo | 157 |
7 | adamant | 156 |
8 | TheScrasse | 154 |
8 | nor | 154 |
10 | Dominater069 | 153 |
I want to delete some element in a priority queue not at the top, how should i do? can someone suggest any method, in constant or logarithmic time. How to handle duplicates while removing
Name |
---|
It's not possible. In a heap(or pq) finding an element in itself is linear.
OK! can we implement it on our own such that it work in logarithmic time, is it possible?
Use multiset
but suppose we use multiset which consists of s = {3 , 2 , 2 }, now if we want to delete just one 2 from it, its not possible because i think both 2's will be deleted.
Use s.erase(s.find(2)) . It will delete only one 2.
Use set or multiset . Now to get the element on top you can call set_name.rbegin() . To delete some elements just delete it if you know that they are in set . Learn about how to delete an element in mulitset because if you delete an element in multiset by value it will delete all the elements with that value .
yes, that's what i am saying. how to delete just one element from multiset.
I think now you know .
Is it possible to delete any random elements from multiset ,suppose multiset contains 10 elements , so is it possible to delete, say 5th element?
This can be possible with the help of ordered set made up in C++.
Order static tree
use multiset instead
You can remove the top easily.
If you want to delete another element, you can do it "lazily": when you are given an element to delete from the priority_queue, store somewhere else that this element no longer belongs there (for examplein a map or in a vector of booleans). Then, when you have a "top" request on the priority queue, pop until you reach an element actually still in the queue, according to the other data structure.
Yes