G - Worst Picture Editorial by en_translator
Unless the answer is \(N\), point \(p\) is always on one of the \(O(N^2)\) lines connecting two people. Let \(S\) be a set of such liens that intersects the region \(x<0\).
If \(|S|=0\) (i.e. if \(X_i\) are all equal), the answer is \(N\).
If \(|S|>0\), the candidates for \(p\) is a point on a line in \(S\). We can check the cases where \(p\) is on exactly one of the lines in \(S\) in total of \(O(N^3)\) by counting the number of people in the picture in an \(O(N)\) time for each line. For the cases where it belongs to more than one line, it is sufficient to check the intersection points. At worst, \(|S|=\Theta(N^2)\), so there are \(\Theta(N^4)\) candidates. A naive algorithm of counting the number of people in a picture requires a total of \(\Theta(N^6)\) time. Even with this solution, your code will be accepted with a decent margin from the execution time limit as long as you use a fast language; but we also consider a faster way.
We perform an \(O(N^3)\) precalculation of counting the number of people in each line in \(S\).
Then we prepare an associative array whose keys are the coordinates of the points and values are the set of liens passing through the point. We update this associative array while iterating through the \(O(N^4)\) lines that are the intersections of two lines in \(S\). In total, the answer can be found in a total of \(O(N^4 \log N)\) time.
We can find the intersection of two lines in a three-dimensional space by finding the projection of the two lines on the plane \(z=0\) to find the intersection on the \(xy\)-plane, and then finding the \(z\) coordinates at this point. However, you have to handle appropriately the cases where two lines coincide as a result of the projection (an easy solution is to project to another plane, like \(y=0\) or \(x=0\)).
To avoid being affected by errors, it is recommended to manage the coordinates as a rational number. Since the coordinates in the input are small enough, we can skip deduction to speed it up. (In an appropriate implementation, the values in the process is about fourth power of the coordinates.)
Writer’s solution (C++) with deduction (1200ms)
Writer’s solution (C++) without deduction (50ms)
Writer’s solution (Python)
posted:
last update: