Пожалуйста, прочтите новое правило об ограничении использования AI-инструментов. ×

Блог пользователя spirited_away_

Автор spirited_away_, история, 5 лет назад, По-английски

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

  • Проголосовать: нравится
  • 0
  • Проголосовать: не нравится

»
5 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

It's not possible. In a heap(or pq) finding an element in itself is linear.

  • »
    »
    5 лет назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    OK! can we implement it on our own such that it work in logarithmic time, is it possible?

    • »
      »
      »
      5 лет назад, # ^ |
        Проголосовать: нравится 0 Проголосовать: не нравится

      Use multiset

    • »
      »
      »
      5 лет назад, # ^ |
        Проголосовать: нравится 0 Проголосовать: не нравится

      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 .

»
3 года назад, # |
  Проголосовать: нравится -13 Проголосовать: не нравится

Is it possible to delete any random elements from multiset ,suppose multiset contains 10 elements , so is it possible to delete, say 5th element?

»
3 года назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

use multiset instead

»
2 года назад, # |
  Проголосовать: нравится +66 Проголосовать: не нравится

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.