mardi 5 juillet 2016

ith order statistic using c++ STL

Given an empty array, I need to make two type of queries

  1. inserting an element in the array
  2. finding the index of some element k (obviously the array has to be kept sorted)

This can be done be using set container

set<int> st;
set.insert(t);

This will insert my element in O(log(n)).

And for 2nd query

set<int>::iterator it;
it = st.find(k);
idx = distance(st.begin(), it);

This takes O(n) time. (O(n) {for distance()} + O(log(n) {for set::find()} )

Is there any way to do both queries in O(log(n)) using the predefined containers of C++?

http://www.cplusplus.com/reference/stl/

Aucun commentaire:

Enregistrer un commentaire