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

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

There is a list of key vs value pair.size = n. No I have been given a key I have to find the max value from the list such that its key is less than the given key.

If there are q queries I can do this in O(qn). Is there a faster way to do this.

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

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

Let each key be an index of the corresponding value. Store the values in a segment tree for maximum. Then, if you need to find the maximum value with key < k you just need to query maximum value on the segment [0;k - 1]. Probably, you may need to compress key values so they would fit the range [0;n].

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

If I am right understood the problem, you should use the Fenwick tree with maximum.