1 / 18

Toothpicks

Toothpicks. HKOI 2012 Senior Q2. Problem. N non-overlapping rectangles Using toothpicks to form the shape . Solution. Lets view it in a simple way. Break to 1x1 blocks. Compute the perimeters of the squares. Solution. Compute the perimeter. 4 x (number of squares) ?. Solution.

hea
Download Presentation

Toothpicks

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Toothpicks HKOI 2012 Senior Q2

  2. Problem • N non-overlapping rectangles • Using toothpicks to form the shape

  3. Solution • Lets view it in a simple way Break to 1x1 blocks Compute the perimeters of the squares

  4. Solution • Compute the perimeter 4 x (number of squares) ?

  5. Solution • Compute the perimeter Overlapped.. We counted twice 4 x (number of squares) ?

  6. Solution • Compute the perimeter Overlapped.. We counted twice 4 x (number of squares) – Overlapped lines

  7. Solution • Counting overlapped length xx.x xxxx Draw the squares on a ‘map’ A 2D array!

  8. Solution • Counting overlapped length xx.x xxxx Count how many adjacency ‘x’s! xx.x xxxx xx.x xxxx xx.x xxxx xx.x xxxx xx.x xxxx xx.x xxxx xx.x xxxx

  9. Solution • How about negative coordinates? Pascal: Easy. var a:array[-100..100][-100..100] of boolean; Shift all rectangles by adding 100 to them! C/C++?

  10. Algorithm 1 • Break the rectangles to 1x1 squares • Draw the squares on a map • Count the overlapped length Time complexity? Number of 1x1 squares Depend on size of rectangles In 50% test cases, absolute values of coordinates is at most 100 Work for 50% test cases

  11. Speed up • Do the easy part first - fill the inner part of the rectangle m Number of toothpicks needed for a n x m rectangle = 2nm + n + m n

  12. Speed up • How about the overlapped length? First check which lines are overlapping..

  13. Speed up • How about the overlapped length? First check which lines are overlapping..

  14. Speed up • How about the overlapped length? Compute the overlapped length

  15. Solution • Break each rectangle into 2 vertical line and 2 horizontal line For each pair of line, compute their overlapped length

  16. Algorithm 2 • Sum the toothpicks needed for inner part • Break N rectangles to 2N horizontallines and 2N vertical lines • For each pair of lines, compute their overlapped length (if any), subtract it from answer Time complexity: O(N2)

  17. Extra – We can do faster For each pair of line, compute their overlapped length Do we really need to go through all pair of lines?

More Related