atcoder#ABC230D. [ABC230D] Destroyer Takahashi
[ABC230D] Destroyer Takahashi
Score : points
Problem Statement
In a town divided into a grid with rows and columns, there are walls, numbered to . Wall ranges from the -th column to the -th column from the left in the -th row from the top. (See also the figure at Sample Input and Output .)
Takahashi has decided to destroy all walls. With his superhuman strength, his one punch can damage consecutive columns at once.
- More formally, he can choose an integer between and (inclusive) to damage all walls that exist (or partly exist) in the -th through -th columns and are not yet destroyed.
When a part of a wall is damaged, that whole wall will be destroyed by the impact of the punch. (See also the figure at Sample Input and Output .)
At least how many times does Takahashi need to punch to destroy all walls?
Constraints
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
Output
Print the minimum number of punches needed to destroy all walls.
3 3
1 2
4 7
5 9
2
The figure below describes the arrangements of walls in Sample Input .
He can destroy all walls with two punches, such as the following. (Below, denotes the range from the -th through -th columns.)
- First, punch . The walls existing in ― Walls and ― are damaged and destroyed.
- Second, punch . The wall existing in ― Wall ― is damaged and destroyed.
It is also possible to destroy all walls with two punches in this way:
- First, punch to destroy Walls and .
- Second, punch to destroy Wall .
3 3
1 2
4 7
4 9
1
The difference from Sample Input/Output is that Wall now covers , not . In this case, he can punch to destroy all walls with one punch.
5 2
1 100
1 1000000000
101 1000
9982 44353
1000000000 1000000000
3