Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <ol> <li><p>Yes it can.</p></li> <li><p>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 <strong>N</strong> stones being <em>alive</em>, and at each step <em>kill</em> the appropriate stone in our tree. Only need to know which element we're currently at (I think it's appropriate to call it <strong>m</strong>). The high-level algorithm (I leave the details to you) is as follows (<strong>P</strong> is our data structure):</p> <pre><code>While P.size &gt; 1: P.toggle(m) // remove the m-th stone m = P.kth(next stone to be killed) </code></pre> <p>P.size in the code above is simply the number of all remaining stones. In the description below, it corresponds to tree[1].</p> <p><strong>Note:</strong> The symbol <strong>k</strong> used in the data structure is different from the one in the problem input that represents <em>jump distance</em>.</p></li> <li><p>Pretty much. I haven't seen that name before, but the code looks the same as what I've seen people call a <strong>population tree</strong>.</p> <p>The population tree is a simple way to use the segment tree. You have <strong>N</strong> elements each having two possible states: 1 for alive and 0 for dead. The tree supports two operations:</p> <ul> <li>Toggle the state of element numbered <strong>i</strong>.</li> <li>Return the index of the <strong>k-th</strong> (in size of its index) living element. <br/><br/></li> </ul> <p>To clarify the second operation let's say that the set of living elements is {1, 2, 4, 7}. If <strong>N</strong> = 8, that corresponds to the state array 01101001 (element 0 is dead, element 1 is alive, element 3 is alive, and so on).</p> <p>So, how to implement this? As usual, leafs of the tree correspond to the <em>array</em>. 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.</p> <p>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:</p> <pre><code>const int size = 1 &lt;&lt; 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 &gt; 0; i /= 2) tree[i] = tree[2 * i] + tree[2 * i + 1]; } </code></pre> <p><strong>Note:</strong> a common way of labeling nodes is to have the <em>root</em> equal to 1, and recursively, the children of a node <strong>n</strong> are <strong>2n</strong> and <strong>2n+1</strong>.</p> <p>To find the <strong>k-th</strong> living element we can think recursively: We're currently at node <strong>n</strong>, and are looking for the <strong>k-th</strong> living element its subtree (the subtree of a node is the tree rooted at that node). If <strong>n</strong>'s left child <strong>2n</strong> has <strong>k</strong> or more living elements, set <strong>n</strong> = <strong>2n</strong>. Otherwise, we'll obviously go to the right child, that is set <strong>n</strong> = <strong>2n+1</strong>. In that case, we're no longer looking for the <strong>k-th</strong> living element of the subtree. Because we skipped all the living elements of the left child, we subtract that count from <strong>k</strong>. Here we're looking at the living elements 1-based, for simplicity.</p> <p>The basic thinking here might be recursive, but the description hints that doing it iteratively should also be quite simple:</p> <pre><code>int kth(int k) { ++k; // because this method looks at elements 1-based int current_node = 1; // start at the root while (current_node &lt; size / 2) { if (tree[2 * current_node] &gt;= 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 } } } </code></pre> <p>These two functions are what makes that <strong>segment tree</strong> a <strong>population tree</strong>.</p></li> </ol> <p>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 <em>Josephus problem</em>, and has alternative solutions, so you might be interested in looking it up.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload