Given an empty array, I need to make two type of queries
- inserting an element in the array
- 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++?
Aucun commentaire:
Enregistrer un commentaire