2

recently I came across the following problem: Consider a string of $n \geq 3$ blank squares. Start by colouring the leftmost square and rightmost square. Now, consider the following protocol to colour squares: at each iteration, you colour the square that has a maximal distance to its two closest squares, and in case of equality you choose the one on the left. You colour this square as long as its two adjacent squares aren't coloured yet.

To make the problem more intuitive I should probably mention that it is a variant of the classical urinary problem, in which you are given n urinaries and each person choses the urinary that is the furthest apart from all the urinaries that are already used, and in case of equality, you choose the one on the left (closest to the door, for instance).

My question is for each $n$, how many urinals are filled. I can't seem to find an easy recurrence whatsoever, so any help would be appreciated.

  • I believe it is easy to see that the number $c$ of coloured squares satisfies $c \leq \lfloor n/2 \rfloor$, no? – RGS Oct 25 '17 at 20:34
  • @RGS unless I'm missing something, the $n=5$ case breaks your inequality – jameselmore Oct 25 '17 at 20:40
  • @jameselmore nope you are not, I am terrible with the $\pm 1$ I often need, I forgot my $+1$ here :P – RGS Oct 25 '17 at 20:43

2 Answers2

0

After the ends are colored, you operate in a run of $m$ blank squares between colored ones. If $m$ is odd, you color a square and replace it with two runs of $\frac 12(m-1)$. If $m$ is even, you color a square and replace it with a run of $\frac m2$ and a run of $\frac m2-1$ With the clarification that you can't color a cell next to any colored cell, with $m=1$ or $m=2$ you can't color any. Our recurrence becomes $$T(m)=\begin {cases} 0&m=1\\0&m=2\\2T(\frac 12(m-1))+1&m \text{ odd}\\T(\frac m2)+T(\frac m2-1)+1&m \text { even} \end {cases}$$ The solution to your problem is $2+T(n-2)$ because you start by coloring the two end squares, leaving a run of $n-2$ blank cells between colored ones.

Added: I made a spreadsheet to compute the recurrence values. $T(3)=T(4)=T(5)=1, T(6)=2, T(7)$ through $T(11)=3$. The values get stuck at $2^k-1$ for $2^k+1$ numbers, then step by ones up to $2^{k+1}-1$, which repeats $2^{k+1}+1$ times. So $T(15)$ through $T(23)$ are $7$, then we step up to $T(31)=15$ and get stuck at $15$ through $T(47)$ then step up to $T(63)=31$ and stay there through $T(95)=31$.

The run of $2^k-1$ extends from $2^{k+1}-1$ through $2^{k+1}+2^k-1$. We can use that to write a nonrecursive algorithm for $T(m)$. Given $m$, let $k=\lfloor \log_2(m+1) \rfloor-1$. If $m \le 2^{k+1}+2^k-1, T(m)=2^k-1$. If $m \gt 2^{k+1}+2^k-1, T(m)=2^k-1+m-(2^{k+1}+2^k-1)=m-2^{k+1}$. I have not proven that this formula works, just observed that it seems to.

Ross Millikan
  • 374,822
  • Nice, except I think the m = 2 case is also 0, since you can't colour any of the two squares because they would be adjacent to already coloured squares (consider the case n = 4, for instance, where the answer is 2 and with your recurrence I get 3). Also, how do you solve these type of recurrences? – Todd Chavez Oct 25 '17 at 20:46
  • I took the statement "you color this one as long as the two adjacent ones aren't colored yet" to mean one needed to be uncolored. If you need both to not be colored yet, you are correct. I will update it. Note I added a $+1$ to each recurrence line for the cell you color at that stage. – Ross Millikan Oct 25 '17 at 20:58
0

I think the first thing to be noted is that whenever you colour a square, what happens to the left of it is independent of what happens to the right of it, right? Thus, even if that isn't the actual order in which the squares are coloured, you can think of colouring the mid square, then everything to the left of it. Then everything to the right of it.

To colour the left-hand side, colour the mid square, everything to the left of it, then to the right. And so on, and so forth..

Let $C_n$ denote the number of coloured squares when you have $n$ squares, without taking into account the two ends. So, for example, $C_3 = 0$.

If $n$ is odd and big enough, $C_n = 1 + 2C_{(n+1)/2}$ which means you coloured a square, and then are left with colouring the two halfs. If $n$ is even, then you have $C_n = 1 + C_{n/2} + C_{n/2 + 1}$. You also have $C_i = 0, i<5, C_5 = 1$.

The real number you want will be $2 + C_n$, because the two ends are also coloured.

For example, $2 + C_6 = 2 + 1 + C_3 + C4 = 3$ which which matches $c0c00c$ and

$2 + C_7 = 2 + 1 + 2C_4 = 3$ which matches $c00c00c$.

RGS
  • 9,719
  • What you say about the two halves being independent is a correct observation, and I know intuitively the answer is around log(n), but I would like a formal and precise closed formula, thanks. – Todd Chavez Oct 25 '17 at 20:52
  • I believe my answer is formal, even though it is not in closed form. How is it $\log{n}$, though? I would say it is around $n/3$, given that for every $3$ consecutive squares, at least one is coloured. – RGS Oct 25 '17 at 21:02