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

Автор FattyLovesMangoes, 5 лет назад, По-английски

Can someone explain how to solve this and if you know similar problems, can you please mention them here?

I know trie concept but not able to come up with a solution.

(Please don't down vote until some one posted the solution. I don't want it to disappear from recent actions without being answered.)

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

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

I think something like the following could work (lmk about any issues):

Construct the bit trie on all elements, then for each element in the sequence, do the following:

Find the paths in the trie that produce the lowest and highest xor (of course, excluding the path of the current element). Denote these xors as $$$l$$$ and $$$r$$$ respectively. Now, we know that for the current element, we have $$$n - 1$$$ xors in the range $$$[l, r]$$$.

Now, sort all of these $$$[l, r]$$$ segments. We know that there are $$$n$$$ such segments, and that each segment has $$$n - 1$$$ xors in it. This is good for the following brute force:

Sort all the segments. Keep iterating through the segments as long as we can take the next segment without the total # of xors exceeding k. When this condition is broken, simply iterate through all xors of the current segment until we get to the k-th smallest xor overall.

EDIT: Small issue since this solution counts each pair twice. You can solve an equivalent problem where you consider the xor of all ORDERED pairs, and instead find the 2 * k -th smallest xor.

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

This looks like binary search (search the answer) problem. Can you see how you can count number of xors <= lim in a bit trie or does it need clarification?