Editorial for COCI '16 Contest 3 #6 Meksikanac
Submitting an official solution before solving the problem yourself is a bannable offence.
The problem can be broken down into two simpler problems:
- For a given polygon, find all integer points contained in its interior or on the edge.
- For each possible position of the polygon, determine whether a fly exists with its position being equal to an integer point in the polygon, after its translation.
The first part of the algorithm will be solved using ray casting. A point is located within a polygon if and only if an arbitrary ray from that point intersects with the polygon in an odd number of points. However, there are special cases, because the ray can touch a polygon's vertex, and can contain a polygon's side. For details, consult the following post.
At the same time, we will check all integer points having the same coordinate. We can find the intersections of each polygon's side with a vertical line through that coordinate, and use the sweep line algorithm to check, for each point on the line, if they are inside of the polygon. This way, the complexity of the first part of the algorithm will be . But, we can perform the entire procedure in only one sweep, by maintaining a set of lengths, so we don't have to sort the intersections for each coordinate. The complexity of this procedure is .
We will reduce the second part of the algorithm to a problem in one dimension. Let denote all integer points within a given polygon translated such that its smallest and coordinates are . We can represent these points with a binary string , where we write at position for each of the points inside of the polygon, and write for the remaining positions in the string. Now, translating the polygon for corresponds to translating the string for . We define the string that represents the flies in the same way.
We are left with determining, for each possible polygon translation, whether the bitwise and of the translated string and string is equal to . We can do this by using a bitset in the complexity of , but with a very small constant. But, a faster solution exists: we will reverse the string , and observe strings and as polynomials in the standard notation. From their product, we can make out the required information. The details of this approach are left as an exercise to the reader. If we implement the multiplication of polynomials using FFT, this part of the algorithm is of the complexity .
The total complexity of the algorithm is .
Comments