Steel Slicing 2
Time limit1sMemory limit1024 MB
Given a histogon of width N made from two mirrored histograms, find the fewest axis-parallel cuts that split it into rectangles.
- Level
Hard8 of 10
- Topics
- Geometry, Greedy, Implementation
- Solved
- No attempts yet
Problem
ISCO (ICPC Steel Company) buys steel sheets of a certain shape, cuts them into pieces, and sells them in the industry market. Each steel sheet that ISCO buys is shaped like two histograms of equal width, where one histogram is reflected vertically and soldered to the bottom of the other. This forms a polygon without holes, and every side is either horizontal or vertical. We call such a polygon a histogon. See the figure below for a histogon.

A piece sells for much more if it is rectangular, so it is better to cut a steel sheet into several rectangles. A laser cutter does the cutting.
In a single operation, the laser cutter can trace either a horizontal or a vertical segment through a polygon. The segment must touch the polygon's border at exactly two points, the endpoints of the segment. After the cut, the polygon is split into two smaller polygons along the traced path. The laser cutter can only operate on a single polygon in one operation.
The laser cutter is expensive to use. Find the minimum number of laser cutter operations needed so that all resulting polygons are rectangles.
Input
The first line contains , the width of the histogon ().
Each of the next lines contains two integers and . is the height of the -th column of the histogram that is not reflected, and is the height of the -th column of the reflected histogram. ()
Output
Print a single integer, the minimum number of laser cutter operations needed.