Cow Steeplechase
From USACO. Shape / Geometry. Solve the geometry problem "Cow Steeplechase".
Examples
Input: [13,18,20,18,22,10,22,9,24,5,16,5,18,8,18,27,5,20,13,20,13,30,13,10]
Output: 2
Input: [28,25,8,25,30,24,30,25,23,16,24,16,26,3,26,1,26,1,27,1,19,20,19,26,18,18,7,18,14,7,14,15,26,2,10,2,18,2,18,9]
Output: 3
Hints
Horizontal fence (x1,x2,y) and vertical fence (x,y1,y2) intersect iff vertical.x is in [horizontal.x1, horizontal.x2] AND horizontal.y is in [vertical.y1, vertical.y2]. Model as bipartite graph: horizontals on left, verticals on right, edge = intersection.
Minimum fences to remove so no intersections remain = minimum vertex cover in bipartite graph. By Kőnig's theorem, min vertex cover size = max matching size.
N ≤ 250, O(V·E) DFS augmenting path matching works. Fences can be points (x1=x2 or y1=y2) — still valid, use inclusive bounds. Answer = max matching cardinality.
Related Problems
Cow Steeplechase
**From USACO.** Shape / Geometry. Solve the geometry problem "Cow Steeplechase".