Editorial for COCI '15 Contest 2 #3 Artur
Submitting an official solution before solving the problem yourself is a bannable offence.
Let us first notice that there is always going to be at least one stick that can be removed from the table in a manner that follows the rules of the game. In other words, a solution always exists.
The first step towards solving the task is answering the question: "Is stick in the way of moving stick ?". We observe the projections of the corresponding line segments on the -axis. A projection of a line segment with ending points and corresponds to the line segment with ending points and . If the observed projections don't have points in common, then the removal of sticks and is mutually independent. Otherwise, let denote one of the mutual points of two projections. Let denote the intersection of line with stick , and denote the intersection of that line with stick . Stick is in the way of moving stick if is located above . Therefore, the answer to the given question is obtained in .
Let us imagine that each line segment corresponds to a node in a directed graph and that there is an edge from node to node if and only if stick is in the way of moving stick . Now we need to find a sequence of nodes so that it holds for each directed path between and that node is located before node in that sequence. This order of nodes is called topological sort and it is found using a slight modification of depth-first search (DFS). More precisely, after we have expanded from one node to all its neighbours, we add that node to the end of the sequence. This way, we have made sure that all line segments in the way of moving the current one are removed before we remove the current line segment.
The time complexity of this algorithm is because of building the graph. The topological sort itself is as efficient as a DFS traversal of the graph.
Comments