Question

The link to the question is UVA - 1394 : And There Was One.
The naive algorithm is to scan the entire array and marking the kth element on each iteration stopping at the last : this takes O(n^2) time.
I have searched for an alternative algorithm and came across a chinese blog which pointed me to segment trees using lazy propogation as a solution for O(N lgN) time.
Having studied segment trees I tried forming an algorithm for O(N lgN) time but to no avail.


My questions are :
1. Can a segment tree be used to get the desired runnig time?
2. If yes, enlighten me as to how to use them.
3. Are segment trees and "zkw" segment trees one and the same? - he mentions zkw segment trees in the blog.
UPDATE: The above problem is an example of the Josephus problem..

Was it helpful?

Solution

  1. Yes it can.

  2. You can see the description of the data structure below, here I'll just hint how to use it in the given problem. The population we're representing is obviously the circle of stones. We start with all of the N stones being alive, and at each step kill the appropriate stone in our tree. Only need to know which element we're currently at (I think it's appropriate to call it m). The high-level algorithm (I leave the details to you) is as follows (P is our data structure):

    While P.size > 1:
      P.toggle(m) // remove the m-th stone
      m = P.kth(next stone to be killed)
    

    P.size in the code above is simply the number of all remaining stones. In the description below, it corresponds to tree[1].

    Note: The symbol k used in the data structure is different from the one in the problem input that represents jump distance.

  3. Pretty much. I haven't seen that name before, but the code looks the same as what I've seen people call a population tree.

    The population tree is a simple way to use the segment tree. You have N elements each having two possible states: 1 for alive and 0 for dead. The tree supports two operations:

    • Toggle the state of element numbered i.
    • Return the index of the k-th (in size of its index) living element.

    To clarify the second operation let's say that the set of living elements is {1, 2, 4, 7}. If N = 8, that corresponds to the state array 01101001 (element 0 is dead, element 1 is alive, element 3 is alive, and so on).

    So, how to implement this? As usual, leafs of the tree correspond to the array. That is, the i-th leaf has value 1 if the i-th element is alive, and 0 otherwise. Each internal node remembers the sum of the values of its children.

    To toggle the state of an element, we toggle the value of the corresponding leaf, and then fix on the path from that leaf to the root:

    const int size = 1 << 18; // 2^17 elements, the rest are internal nodes
    int tree[size]; // number of living elements in the subtree of a node
    
    void toggle(int i) {
      tree[i + size / 2] ^= 1; // toggle the leaf
      for (i /= 2; i > 0; i /= 2)
        tree[i] = tree[2 * i] + tree[2 * i + 1];
    }
    

    Note: a common way of labeling nodes is to have the root equal to 1, and recursively, the children of a node n are 2n and 2n+1.

    To find the k-th living element we can think recursively: We're currently at node n, and are looking for the k-th living element its subtree (the subtree of a node is the tree rooted at that node). If n's left child 2n has k or more living elements, set n = 2n. Otherwise, we'll obviously go to the right child, that is set n = 2n+1. In that case, we're no longer looking for the k-th living element of the subtree. Because we skipped all the living elements of the left child, we subtract that count from k. Here we're looking at the living elements 1-based, for simplicity.

    The basic thinking here might be recursive, but the description hints that doing it iteratively should also be quite simple:

    int kth(int k) {
      ++k; // because this method looks at elements 1-based
      int current_node = 1; // start at the root
      while (current_node < size / 2) {
        if (tree[2 * current_node] >= k) 
          current_node = 2 * current_node; // descend into the left child
        else {
          k -= tree[2 * current_node]; // fix k
          current_node = 2 * current_node + 1; // descend into the right child
        }
      }
    }
    

    These two functions are what makes that segment tree a population tree.

This was a data structure question, so the described idea uses a data structure. However, I'd like to mention that the problem is known as the Josephus problem, and has alternative solutions, so you might be interested in looking it up.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top