Home  >  Q&A  >  body text

c++ - n个数,要求插入,查找最大最小值,删除最大最小值的时间复杂度都限制在O(log2n),应该用什么算法?

n个数,要求插入,查找最大最小值,删除最大最小值的时间复杂度都限制在O(log2n),应该用什么算法和数据结构?

黄舟黄舟2765 days ago747

reply all(5)I'll reply

  • 高洛峰

    高洛峰2017-04-17 14:45:26

    The two answers above have different starting points.

    Balanced binary trees can be done, but the original sequence will be sorted.
    In C++, both set and map can meet the requirements.

    The line segment tree can find the maximum or minimum value of the interval without reordering.
    There is no ready-made data structure available, you can write a template yourself.

    reply
    0
  • PHP中文网

    PHP中文网2017-04-17 14:45:26

    Use balanced binary trees

    reply
    0
  • PHP中文网

    PHP中文网2017-04-17 14:45:26

    How do you use a balanced binary tree? Although it is feasible; but if it is only the maximum and minimum values, then you only need to use a heap. There is a ready-made data structure in C++, which is priority_queue in STL.

    reply
    0
  • 迷茫

    迷茫2017-04-17 14:45:26

    Line segment trees can be solved...

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-17 14:45:26

    Use set

    in C++ STL

    reply
    0
  • Cancelreply