Loserinlife's blog

By Loserinlife, history, 4 months ago, In English

Given a n * m matrix of 0s and 1s. For each square,find the sum of Manhattan distances from that square to the first kth nearest 1s.

(Distance to closest 1 + distance to second closest 1 + ...)

n, m <= 2000

k <= n * m

There are at least k ones in the matrix. Thanks!

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

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

    I think it's a bit different because I want the sum of the first kth distances, not only the kth.

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

      the idea is a bit similar, in order to calculate the sum of the first kth distance, we need to consider 4 cases to handle |x1 — x2| + |y1 — y2|.

      (x1 > x2, y1 > y2)

      (x1 < x2, y1 < y2)

      (x1 > x2, y1 < y2)

      (x1 < x2, y1 > y2)

      we can see that we can divide the square into 4 seperate smaller squares to handle that.

»
4 months ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

Can you provide the source of this task? I think I have something quite close to solving the task but I need some research on it