#P2703. [NOI2006] 千年虫

    ID: 1720 远端评测题 1000ms 125MiB 尝试: 0 已通过: 0 难度: 9 上传者: 标签>动态规划,dp贪心2006NOI 系列枚举,暴力

[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 AA of a Millennium Bug in a fossil, find a shape BB that conforms to the theoretical model such that BB is most likely to become AA 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 xx direction; the direction perpendicular to xx is the yy 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 xx 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 xx direction, and whose edge farthest from the trunk is perpendicular to the xx 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 44 legs on the left and 55 on the right.)

In the xx-yy 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 (x,y)(x, y). Let the distance between the head and the tail be nn. Then we can describe a Millennium Bug BB using 2×n2 \times n integers (see the right figure): cut BB into nn horizontal strips of width 11 along directions parallel to the xx axis. In the ii-th strip, let the xx coordinate of the leftmost cell be LiL_i, and the xx coordinate of the rightmost cell be RiR_i. Then (n,L1,L2,,Ln,R1,R2,,Rn)(n, L_1, L_2, \dots, L_n, R_1, R_2, \dots, R_n) 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 (n,L1,L2,,Ln,R1,R2,,Rn)(n, L_1, L_2, \dots, L_n, R_1, R_2, \dots, R_n). Here LiRiL_i \le R_i.

For example, see the following figure:

Your task is: given a description AA of a Millennium Bug in a fossil, find a description BB that satisfies the theoretical model, such that BB can become AA through the corrosion process, and the cost of transforming BB into AA (the number of cells that must be corroded) is minimized. Output this minimum cost.

Input Format

The first line contains an integer nn.

The following nn lines each contain two integers. In line ii, the two integers are LiL'_i and RiR'_i, 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, n100n \le 100, Ri100R'_i \le 100.
  • For 50% of the testdata, n1000n \le 1000, Ri1000R'_i \le 1000.
  • For 70% of the testdata, n105n \le 10^5, Ri1000R'_i \le 1000.
  • For 100% of the testdata, 1n1061 \le n \le 10^6, 0LiRi1060 \le L'_i \le R'_i \le 10^6.

Translated by ChatGPT 5