0

I would like to solve Joseph Flavius problem using segment tree. I'm almost sure that simple simulation (i.e. lined list) is O(n^2). What I want to achieve is jumping on array for particular distance, taken from segment tree. In other words segment tree will keep information about number of deleted elements and taking some info from tree will allow to find next element to delete in O(1). The problem is that I dont know how to storage info in segment tree to make it working for Joseph Flavius problem.

Some kind of extra values keeped in each node? But how to make queries about next element?

Óscar López
  • 232,561
  • 37
  • 312
  • 386
Michocio
  • 503
  • 3
  • 19
  • have you coded anything already ? – guillaume girod-vitouchkina Dec 12 '15 at 09:32
  • Only solution based on linked list, which has too big complexity. – Michocio Dec 12 '15 at 09:33
  • The simplest solution for this problem uses no data structures at all. It starts from the end position (when there is only one person left), and loops to calculate the position of the previously executed person. The second-simplest solution starts from n people and uses recursion. That uses too much stack when n is large, but you can always implement your own stack instead and do the recursion as a loop. – Matt Timmermans Dec 13 '15 at 15:58

1 Answers1

0

The first thought I had was binary search + sement tree of sums giving O(log^2(n))

Jump from L to R has this properties:

R - L + 1 - sum(L, R) == skip_value

You can easily find R with this property using bin-search. It gets a little more complicated when you make a full circle but I believe that you get the idea.

If anything is unclear feel free to ask.

(I will also think about log(n) solution)