Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I don't have enough reputation to leave a comment directly, so I'll be detailed and leave an answer here. </p> <p>Your algorithm does pair wise computation with a nested loop and has O(n<sup>2</sup>) temporal complexity, that's why your algorithm is slow for large inputs (large here doesn't mean the coordinate values, but the number of light houses). First let's see what we could do to optimize, with a sample input:</p> <p>3<br/>1 1 (P<sub>a</sub>)<br/>2 2 (P<sub>b</sub>)<br/>3 3 (P<sub>c</sub>)</p> <p>With your algorithm, the execution logic would be:</p> <ol> <li>count=0</li> <li>P<sub>a</sub> and P<sub>b</sub> is a pair? True, count++</li> <li>P<sub>a</sub> and P<sub>c</sub> is a pair? True, count++</li> <li>P<sub>b</sub> and P<sub>c</sub> is a pair? True, count++</li> <li>output count</li> </ol> <p>There are actually redundant computations which could be eliminated:</p> <p>Determine whether a newly added light house could be illuminated by existing light houses. <br/><br/>Note that P<sub>a</sub>'s NE region actually contains P<sub>b</sub>'s NE region, so if a new light house falls into P<sub>b</sub>'s NE region, that implicitly means it's illuminated by P<sub>a</sub>, as well as P<sub>b</sub>. So if we have P<sub>a</sub> and P<sub>b</sub> in the sea and add P<sub>c</sub>, we actually don't need to compute twice with P<sub>a</sub> and P<sub>b</sub> separately. </p> <p>Say if we have the following record which records the the illumination intersection areas of light houses: <br/>a. count=0; R={}<br/>b. add P<sub>a</sub>, R={[(-∞,-∞),(1,1)]->[P<sub>a</sub>],[(1,1),(∞,∞)]->[P<sub>a</sub>]} ([(-∞,-∞),(1,1)] and [(1,1),(∞,∞)] define two rectangular areas with their diagonal points)<br/>c. add P<sub>b</sub>, P<sub>b</sub> is in [(1,1),(∞,∞)] (that means [P<sub>a</sub>] can illuminate P<sub>b</sub>), find all light houses in [P<sub>a</sub>] that could be illuminated by P<sub>b</sub>, that's P<sub>a</sub>, count+=1, R={[(-∞,-∞),(1,1)]->[P<sub>a</sub>,P<sub>b</sub>],[(-∞,1),(1,2)]->[P<sub>b</sub>],[(1,-∞),(2,1)]->[P<sub>b</sub>],[(1,1),(2,2)]->[P<sub>a</sub>,P<sub>b</sub>],[(1,2),(2,∞)]->[P<sub>b</sub>],[(2,1),(∞,2)]->[P<sub>b</sub>],[(2,2),(∞,∞)]->[P<sub>a</sub>,P<sub>b</sub>]}<br/>d. add P<sub>c</sub>, P<sub>c</sub> is in [(2,2),(∞,∞)], find all light houses in [P<sub>a</sub>,P<sub>b</sub>] that could be illuminated by [P<sub>a</sub>,P<sub>b</sub>], that's P<sub>a</sub>,P<sub>b</sub>, count+=2, update R (will be too long so I'll just omit it here)</p> <p><img src="https://i.stack.imgur.com/g3bLt.png" alt="enter image description here"></p> <p>One data structure you may want to represent R is a segment tree<a href="https://i.stack.imgur.com/g3bLt.png" rel="nofollow noreferrer">1</a>. The segment tree applicable here would be two-dimensional. I suggest you have a look at existing post like <a href="https://stackoverflow.com/questions/17225753/how-to-code-2d-segment-tree">How to code 2D segment tree?</a> and try to implement your own. </p> <p><a href="https://i.stack.imgur.com/g3bLt.png" rel="nofollow noreferrer">1</a> <a href="http://en.wikipedia.org/wiki/Segment_tree" rel="nofollow noreferrer">http://en.wikipedia.org/wiki/Segment_tree</a></p>
    singulars
    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. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    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