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 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
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?
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 )
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
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 )
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
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
pinball end up? 7 Problem 3 – Pachinko Given N ≤ 10 5 non-intersecting, non-horizontal line segment obstacles, where does the
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
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
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
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
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
around the center of the rectangle! Observation: line must go through center. Do an “angular sweep” by rotating a line 11 Problem 4 – Solution
around the center of the rectangle! Observation: line must go through center. Do an “angular sweep” by rotating a line 11 Problem 4 – Solution
around the center of the rectangle! Observation: line must go through center. Do an “angular sweep” by rotating a line 11 Problem 4 – Solution
around the center of the rectangle! Observation: line must go through center. Do an “angular sweep” by rotating a line 11 Problem 4 – Solution
around the center of the rectangle! Observation: line must go through center. Do an “angular sweep” by rotating a line 11 Problem 4 – Solution
around the center of the rectangle! Observation: line must go through center. Do an “angular sweep” by rotating a line 11 Problem 4 – Solution
around the center of the rectangle! Observation: line must go through center. Do an “angular sweep” by rotating a line 11 Problem 4 – Solution
around the center of the rectangle! Observation: line must go through center. Do an “angular sweep” by rotating a line 11 Problem 4 – Solution
around the center of the rectangle! Observation: line must go through center. Do an “angular sweep” by rotating a line 11 Problem 4 – Solution
around the center of the rectangle! Observation: line must go through center. Do an “angular sweep” by rotating a line 11 Problem 4 – Solution
around the center of the rectangle! Observation: line must go through center. Do an “angular sweep” by rotating a line 11 Problem 4 – Solution
around the center of the rectangle! Observation: line must go through center. Do an “angular sweep” by rotating a line 11 Problem 4 – Solution
around the center of the rectangle! Observation: line must go through center. Do an “angular sweep” by rotating a line 11 Problem 4 – Solution
around the center of the rectangle! Observation: line must go through center. Do an “angular sweep” by rotating a line 11 Problem 4 – Solution
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.
13 Find the circle of radius R that contains the most points. Problem 5 – Angular sweep with circle
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
Recommend
More recommend