Editorial for Baltic OI '10 P1 - BEARs
Submitting an official solution before solving the problem yourself is a bannable offence.
Let's begin with a somewhat easier task: given an integer , we need to find out whether the sheriff can keep the BEARs at least distance from the warehouse. In other words, determine if the sheriff can block streets in such a way that the BEARs cannot enter any intersection strictly bounded by a square with vertices at . Let be the set of intersections within this square (excluding intersections on edges and vertices).
Let be the set of intersections such that if the BEARs start in an intersection in , they will be able to reach an intersection in no matter what the sheriff will do. We can express as the minimal set of intersections such that any intersection is in set if at least one of the following holds:
- is in ;
- there is a main street segment from leading to an intersection in ;
- at least two of the neighbouring intersections are in .
Note that because of 3, will always be a rectangle.
We can construct the set iteratively. Let's begin with . Then for every main street that has at least one intersection in , we extend so that it would include all intersections in that main street. We do this until there are no main streets to add.
Now, if the intersection is in , the BEARs will reach an intersection in .
We can now use binary search to find the maximum value of for which the BEARs won't be able to reach .
We construct the set in , thus we solve the task in .
Comments