#P2703. [NOI2006] 千年虫
[NOI2006] 千年虫
Description
The Millennium Bug is an ancient creature that vanished from Earth tens of millions of years ago, and little is known about it. Paleobiologists have recently become interested in it because a set of precious Millennium Bug fossils was discovered, preserving the creature’s shape nearly intact.
Theoretical scientists have generalized a model of the Millennium Bug’s shape from these fossils, and on this basis determined that the Millennium Bug is the ancestor of the centipede. However, Scientist J found discrepancies between the model and reality. After closely studying hundreds of fossils, he discovered that most of them do not completely conform to the theoretical model. What caused this? Theoretical Scientist K pointed out that the shape of a Millennium Bug preserved in a fossil may have undergone various changes during fossilization, and even the slightest change could make it fail to match the model.
Thus a new problem arises for the scientists: how far is a fossilized Millennium Bug from the theoretical model? Specifically, given the shape of a Millennium Bug in a fossil, find a shape that conforms to the theoretical model such that is most likely to become during fossilization.
The theoretical “Millennium Bug shape model” is as follows (see the left figure): the body consists of four parts — head, tail, trunk, and legs.

- The head and the tail are represented by a pair of parallel line segments. The direction parallel to the head and tail is called the direction; the direction perpendicular to is the direction.
- Between the head and the tail, there are two disjoint polylines connecting them. Together with the head and tail segments, they bound a region called the trunk. Both polylines satisfy all of the following:
- Every turn is an obtuse angle or a straight angle.
- Each polyline consists of an odd number of segments.
- Counting segments from top to bottom, the odd-numbered segments are perpendicular to the direction.
- On each polyline, a leg grows from every even-numbered segment (counted from top to bottom) on the side away from the trunk. Each leg is a trapezoid or rectangle whose top and bottom bases are parallel to the direction, and whose edge farthest from the trunk is perpendicular to the direction.
Note: A leg must not degenerate into a triangle (i.e., both base lengths are strictly greater than zero). The numbers of legs on the two sides of the trunk may differ. (In the above figure, there are legs on the left and on the right.)
In the - Cartesian coordinate system, the boundary of the solid region formed by the trunk and all legs is parallel or perpendicular to the coordinate axes. For convenience, we assume all such boundary lengths are positive integers. Therefore, we can regard each Millennium Bug’s body as a shape formed by unit squares. Each unit square is uniquely determined by coordinates . Let the distance between the head and the tail be . Then we can describe a Millennium Bug using integers (see the right figure): cut into horizontal strips of width along directions parallel to the axis. In the -th strip, let the coordinate of the leftmost cell be , and the coordinate of the rightmost cell be . Then determines a Millennium Bug.
Due to the erosion of time, the shape of the Millennium Bug in actual fossils does not follow the rules of the theoretical model. Some cells of the body have been dissolved and corroded by minerals. Geologists, physicists, and biologists have jointly concluded that:
- Corrosion happens at the granularity of cells; only an entire cell can be corroded.
- Corrosion proceeds step by step, and at each step only one cell is corroded.
- If removing a cell would disconnect the body, that cell will not be corroded at that time.
- If both the left neighbor and the right neighbor of a cell have not been corroded, then that cell will not be corroded at that time.
- The cells adjacent to the head cannot all be corroded, and the cells adjacent to the tail cannot all be corroded.
If the above five rules are satisfied, we can still describe the shape of a Millennium Bug in a fossil using . Here .

For example, see the following figure:

Your task is: given a description of a Millennium Bug in a fossil, find a description that satisfies the theoretical model, such that can become through the corrosion process, and the cost of transforming into (the number of cells that must be corroded) is minimized. Output this minimum cost.
Input Format
The first line contains an integer .
The following lines each contain two integers. In line , the two integers are and , separated by a space. It is guaranteed that the input testdata is valid.
Output Format
Output a single line containing one integer, the minimum cost.
7
4 4
3 4
3 5
1 3
2 2
2 4
3 3
3
Hint
Sample explanation:
As shown in the figure:

Scoring:
There is no partial score. Your program receives full score only if its output exactly matches our answer; otherwise you receive no score.
Constraints:
- For 30% of the testdata, , .
- For 50% of the testdata, , .
- For 70% of the testdata, , .
- For 100% of the testdata, , .
Translated by ChatGPT 5
京公网安备 11011102002149号