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

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

How to find kth largest element each time where k will vary and array is modifiable. That is you can add new elements to the array array can have duplicates.

for example say array is 10 , 20 , 15.

2nd largest = 15

add 17 to array

array becomes 10 15 17 20

3rd largest = 15

Q <= 10^5.

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

»
7 лет назад, скрыть # |
← Rev. 2  
Проголосовать: нравится +3 Проголосовать: не нравится

You can use PBDS

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

Ordered_multiset

Just change less to less_equal

Like this:

#define ordered_set tree<int , null_type,less_equal<int >, rb_tree_tag,tree_order_statistics_node_update> 
#include <ext/pb_ds/assoc_container.hpp> 
#include <ext/pb_ds/tree_policy.hpp> 
using namespace __gnu_pbds; 

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

Try implementing it with fenwick tree. If all the values are less than 1e5 then it becomes relatively easy. If the values can go high then you need to store the queries and process the elements offline so that you can compress the higher values to smaller values.Every element will be compressed to some element <=1e5 as the no. of unique values <= 1e5. Now you can use binary search on fenwick tree !!