DottedCalculator's blog

By DottedCalculator, history, 16 months ago, In English

Problem:

Let $$$n$$$ be a positive integer. A Japanese triangle consists of $$$1 + 2 + \dots + n$$$ circles arranged in an equilateral triangular shape such that for each $$$i = 1$$$, $$$2$$$, $$$\dots$$$, $$$n$$$, the $$$i^{th}$$$ row contains exactly $$$i$$$ circles, exactly one of which is coloured red. A ninja path in a Japanese triangle is a sequence of $$$n$$$ circles obtained by starting in the top row, then repeatedly going from a circle to one of the two circles immediately below it and finishing in the bottom row. Here is an example of a Japanese triangle with $$$n = 6$$$, along with a ninja path in that triangle containing two red circles.

Given a Japanese triangle, find the maximum number of red circles in a ninja path.

I found an algorithm using DP that can solve this problem in $$$O(n^2)$$$. Is there a more efficient solution?

  • Vote: I like it
  • +45
  • Vote: I do not like it

»
16 months ago, # |
  Vote: I like it 0 Vote: I do not like it

In the given example, Is the answer not 4 red circles.

  • »
    »
    16 months ago, # ^ |
      Vote: I like it +5 Vote: I do not like it

    Yes, the answer is 4. The diagram is only supposed to show what a ninja path is.

»
16 months ago, # |
  Vote: I like it +54 Vote: I do not like it
Solution
»
16 months ago, # |
  Vote: I like it +34 Vote: I do not like it

Using Pólya urn model, one can find a probability distribution with which we have an equal probability of visiting each circle at each layer. We conclude, therefore, that the expected value of red circles is $$$1 + 1/2 + \dots + 1/n = O(\log n)$$$, so there always exists a ninja path with $$$O(\log n)$$$.

Actually, a sharper bound holds, $$$\lfloor \log_2 n \rfloor + 1$$$ (one can prove this via dyadic decomposition or its variations, so maybe there is another $$$O(n\log n)$$$ algorithm but which exploits this decomposition), which is reachable via the example of the following kind

       (R)
      (.)(R)
     (R)(.)(.)
    (.)(.)(.)(R)
   (.)(.)(R)(.)(.)
  (.)(R)(.)(.)(.)(.)
 (R)(.)(.)(.)(.)(.)(.)
(.)(.)(.)(.)(.)(.)(.)(R)
  • »
    »
    16 months ago, # ^ |
      Vote: I like it +3 Vote: I do not like it

    Hi,
    as you wrote
    there always exists a ninja path with O(logn)
    How expected number of red balls assures the lenght of ninja path?

    • »
      »
      »
      16 months ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      The expected value generally behaves like the average value. For the last one we have $$$\min A \leq avg A \leq \max A$$$, so if one has the average value $$$x$$$ then there exist both objects that contribute values $$$\geq avg A$$$ and objects that contribute $$$\leq avg A$$$. Of course, this works even if we work via asymptotic expressions rather than via their exact values. To get acquainted with this idea in further depth, I recommend reading about the probabilistic method.

  • »
    »
    16 months ago, # ^ |
    Rev. 3   Vote: I like it 0 Vote: I do not like it

    Ok now my mind is a bit clearer. I think that if we index positions by both diagonals

        1
       2 1
      3 2 1
     4 3 2 1
    
    and
    
        1
       1 2
      1 2 3
     1 2 3 4
    

    and order things by first diagonal in increasing order, breaking ties by the second diagonal in increasing order, then this problem turns into longest non-decreasing subsequence, since from point (i, j) we can go to (i+1, j) or (i, j+1).

    Hopefully I'm not brainfarting once again. Swistakk is right and I'm dumb. This is the simpler way to think about computing the answer from a configuration of balls though.

    • »
      »
      »
      16 months ago, # ^ |
      Rev. 3   Vote: I like it +10 Vote: I do not like it

      That Szekeres bound is wrong. You are fine with non-decreasing, but not that much with non-increasing

      The optimal bound is $$$\lfloor \log_2 n \rfloor +1$$$ and the tight example was already shown above by miaowtin

      • »
        »
        »
        »
        16 months ago, # ^ |
          Vote: I like it +8 Vote: I do not like it

        The IMO problem asks to prove the optimal bound.

        • »
          »
          »
          »
          »
          16 months ago, # ^ |
            Vote: I like it 0 Vote: I do not like it

          The proof of the bound is basically an observation on the DP rows (by how much their sum increases)

          • »
            »
            »
            »
            »
            »
            16 months ago, # ^ |
              Vote: I like it 0 Vote: I do not like it

            I've seen many solutions to this problem, but not this one? Could you elaborate?

            • »
              »
              »
              »
              »
              »
              »
              16 months ago, # ^ |
                Vote: I like it 0 Vote: I do not like it

              The sum of values in a DP row will have to be at least (previous sum + maximum in previous row + 1). This can be shown by looking at the next row left and right of the maximum. The exact lower bound can be derived from this, and the example is tight.

              • »
                »
                »
                »
                »
                »
                »
                »
                16 months ago, # ^ |
                  Vote: I like it 0 Vote: I do not like it

                I have no idea how you get this. I either don't understand what you mean or it's completely false. Imagine a dp row consisting of ten tens. In the row above you will not have values bigger than 11, so the sum will decrease. That's even more evident if you consider the top two rows (i.e. consisting of one and two balls). That's does not work out if you think of "diagonal rows" either

                • »
                  »
                  »
                  »
                  »
                  »
                  »
                  »
                  »
                  16 months ago, # ^ |
                    Vote: I like it +10 Vote: I do not like it

                  I think you don't understand. Here are a few DP rows from the optimal example:

                        x
                       x .
                      . . x
                     x . . .
                    . . x . .
                   . . . . x .
                  . . . . . . x 
                  
                        1
                       2 1
                      2 2 2
                     3 2 2 2
                    3 3 3 2 2
                   3 3 3 3 3 2
                  3 3 3 3 3 3 3
                  
                • »
                  »
                  »
                  »
                  »
                  »
                  »
                  »
                  »
                  16 months ago, # ^ |
                    Vote: I like it 0 Vote: I do not like it

                  Ah ok, you are thinking about dp from top to bottom, I was thinking about dp from bottom to top. All clear now, thanks :)