cpsc 490 problem solving in computer science
play

CPSC 490: Problem Solving in Computer Science of money. You may buy - PowerPoint PPT Presentation

Lecture 18: Geometric interpretations of problems, sweeps Henry Xia, Brandon Zhang based on CPSC 490 slides from 2014-2018 2019-03-21 University of British Columbia CPSC 490: Problem Solving in Computer Science of money. You may buy fractional


  1. Lecture 18: Geometric interpretations of problems, sweeps Henry Xia, Brandon Zhang based on CPSC 490 slides from 2014-2018 2019-03-21 University of British Columbia CPSC 490: Problem Solving in Computer Science

  2. of money. You may buy fractional items (then you will get a fractional part of its attributes). score you can get? Source: ICPC Pacific Northwest Regional 2018 1 Problem 1 – Strategy game You are choosing a combination of n ≤ 10 5 items. You have 1 unit of money to spend. Each item has two attributes x i and y i , and costs 1 unit The total score of your combination of items is ( total x ) · ( total y ) . What’s the maximum

  3. Consider plotting the items in the xy -plane. All of them will lie on the line segment a - b . In general, we can choose combinations that are inside the convex hull of all the items. Now, it’s easy to see that the optimal answer will lie on the boundary of the convex hull. To find it, we can ternary search on each side of the convex hull. Time complexity: O n n 2 Problem 1 – Solution a , ⃗ If we have two items ⃗ b , what possible ( total x , total y ) combinations can we get?

  4. Consider plotting the items in the xy -plane. b . In general, we can choose combinations that are inside the convex hull of all the items. Now, it’s easy to see that the optimal answer will lie on the boundary of the convex hull. To find it, we can ternary search on each side of the convex hull. 2 Problem 1 – Solution a , ⃗ If we have two items ⃗ b , what possible ( total x , total y ) combinations can we get? All of a - ⃗ them will lie on the line segment ⃗ Time complexity: O ( n log n )

  5. make a polyline with them with all slopes positive. 3 Problem 2 – Polyline Input : n ≤ 10 5 points in the plane. Output : the maximum number of points you can choose out of these, such that you can

  6. Note that the points we choose must be non-decreasing in x -coordinate and increasing in y -coordinate. Sort the points by x -coordinate. Then, the answer is the LIS on y -coordinates! Be careful with equal x -coordinates: we should tiebreak by smaller y -coordinate first. 4 Problem 2 – Solution Time complexity: O ( n log n )

  7. Recall the 1D interval union problem. We solved this by scanning from leħt to right, processing “events” (leħt and right endpoints of intervals) in order of increasing x -coordinate, and keeping track of how many intervals were under the sweep line. 5 Line sweeps

  8. The general method for solving sweep problems: • Identify important “events” that occur as we move from leħt to right. • Keep track of some auxiliary data structure that lets us • find the answer, and • update effjciently at each event. 6 Line sweeps

  9. pinball end up? 7 Problem 3 – Pachinko Given N ≤ 10 5 non-intersecting, non-horizontal line segment obstacles, where does the

  10. We’ll find these by sweeping from leħt to right. We only need to know for each segment, which “next segment” the pinball will fall onto. 8 Problem 3 – Solution

  11. We’ll find these by sweeping from leħt to right. We only need to know for each segment, which “next segment” the pinball will fall onto. 8 Problem 3 – Solution

  12. We’ll find these by sweeping from leħt to right. We only need to know for each segment, which “next segment” the pinball will fall onto. 8 Problem 3 – Solution

  13. Data structure: set of lines Events: line segment endpoints Actions: • Leħt endpoint: insert line • Right endpoint: remove line • Lower endpoint of segment: query for the next line below the point 9 Problem 3 – Solution

  14. equal number of blue and red dots. Divide the rectangle into two halves with equal area such that the two sides have an 10 Problem 4 – Angular sweep

  15. around the center of the rectangle! Observation: line must go through center. Do an “angular sweep” by rotating a line 11 Problem 4 – Solution

  16. around the center of the rectangle! Observation: line must go through center. Do an “angular sweep” by rotating a line 11 Problem 4 – Solution

  17. around the center of the rectangle! Observation: line must go through center. Do an “angular sweep” by rotating a line 11 Problem 4 – Solution

  18. around the center of the rectangle! Observation: line must go through center. Do an “angular sweep” by rotating a line 11 Problem 4 – Solution

  19. around the center of the rectangle! Observation: line must go through center. Do an “angular sweep” by rotating a line 11 Problem 4 – Solution

  20. around the center of the rectangle! Observation: line must go through center. Do an “angular sweep” by rotating a line 11 Problem 4 – Solution

  21. around the center of the rectangle! Observation: line must go through center. Do an “angular sweep” by rotating a line 11 Problem 4 – Solution

  22. around the center of the rectangle! Observation: line must go through center. Do an “angular sweep” by rotating a line 11 Problem 4 – Solution

  23. around the center of the rectangle! Observation: line must go through center. Do an “angular sweep” by rotating a line 11 Problem 4 – Solution

  24. around the center of the rectangle! Observation: line must go through center. Do an “angular sweep” by rotating a line 11 Problem 4 – Solution

  25. around the center of the rectangle! Observation: line must go through center. Do an “angular sweep” by rotating a line 11 Problem 4 – Solution

  26. around the center of the rectangle! Observation: line must go through center. Do an “angular sweep” by rotating a line 11 Problem 4 – Solution

  27. around the center of the rectangle! Observation: line must go through center. Do an “angular sweep” by rotating a line 11 Problem 4 – Solution

  28. around the center of the rectangle! Observation: line must go through center. Do an “angular sweep” by rotating a line 11 Problem 4 – Solution

  29. Events: dot crosses over from one side to the other (sorted by angle around center) State: how many of each dot is on each side. 12 Problem 4 – Solution Actions: when a dot hits the line, + 1 or − 1 to the relevant state.

  30. 13 Find the circle of radius R that contains the most points. Problem 5 – Angular sweep with circle

  31. the circle until it is). For every single point, rotate a circle around that point. There is an optimal circle which is tangent to one of the points (otherwise, we can wiggle 14 Problem 5 – Solution

  32. the circle until it is). For every single point, rotate a circle around that point. There is an optimal circle which is tangent to one of the points (otherwise, we can wiggle 14 Problem 5 – Solution

  33. the circle until it is). For every single point, rotate a circle around that point. There is an optimal circle which is tangent to one of the points (otherwise, we can wiggle 14 Problem 5 – Solution

  34. the circle until it is). For every single point, rotate a circle around that point. There is an optimal circle which is tangent to one of the points (otherwise, we can wiggle 14 Problem 5 – Solution

  35. the circle until it is). For every single point, rotate a circle around that point. There is an optimal circle which is tangent to one of the points (otherwise, we can wiggle 14 Problem 5 – Solution

  36. the circle until it is). For every single point, rotate a circle around that point. There is an optimal circle which is tangent to one of the points (otherwise, we can wiggle 14 Problem 5 – Solution

  37. the circle until it is). For every single point, rotate a circle around that point. There is an optimal circle which is tangent to one of the points (otherwise, we can wiggle 14 Problem 5 – Solution

  38. the circle until it is). For every single point, rotate a circle around that point. There is an optimal circle which is tangent to one of the points (otherwise, we can wiggle 14 Problem 5 – Solution

  39. the circle until it is). For every single point, rotate a circle around that point. There is an optimal circle which is tangent to one of the points (otherwise, we can wiggle 14 Problem 5 – Solution

  40. the circle until it is). For every single point, rotate a circle around that point. There is an optimal circle which is tangent to one of the points (otherwise, we can wiggle 14 Problem 5 – Solution

  41. the circle until it is). For every single point, rotate a circle around that point. There is an optimal circle which is tangent to one of the points (otherwise, we can wiggle 14 Problem 5 – Solution

  42. the circle until it is). For every single point, rotate a circle around that point. There is an optimal circle which is tangent to one of the points (otherwise, we can wiggle 14 Problem 5 – Solution

  43. the circle until it is). For every single point, rotate a circle around that point. There is an optimal circle which is tangent to one of the points (otherwise, we can wiggle 14 Problem 5 – Solution

  44. the circle until it is). For every single point, rotate a circle around that point. There is an optimal circle which is tangent to one of the points (otherwise, we can wiggle 14 Problem 5 – Solution

  45. the circle until it is). For every single point, rotate a circle around that point. There is an optimal circle which is tangent to one of the points (otherwise, we can wiggle 14 Problem 5 – Solution

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend