Editorial for COCI '12 Contest 2 #5 Informacije


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.

Let us build a bipartite graph with numbers on the left side and sequence positions on the right. An edge between nodes (x, y) exists by default, except if the provided descriptions make it impossible for number x to be in position y.

Each number can only appear in the intersection of all intervals describing that number, whether defining it as a local minimum or maximum.

Furthermore, we can determine for each position the range of numbers that can be placed in it. We will only consider intervals whose lower bound is less than or equal to the current position, and the right bound greater or equal. Possible numbers in that position range from the largest local minimum to the smallest local maximum of those intervals.

For an edge (x, y) to exist, the position y must be in the intersection of all intervals for x, and x must be in the allowed range for position y.

The only remaining problem is selecting a set of edges such that each number is assigned to exactly one position. It can be solved using a flow/matching algorithm. The problem has sufficiently small bounds to be solvable for all points even using the Ford-Fulkerson method.


Comments

There are no comments at the moment.