#abc230d. D - Destroyer Takahashi
D - Destroyer Takahashi
Score : points
问题描述
在一个被划分为 行和 列网格的小镇上,有 道墙,编号从 到 。 第 道墙位于顶部第 行,从左边第 列延伸至第 列。(参见示例输入与输出 中的图示)
高桥决定摧毁所有这 道墙。凭借其超人的力量,他的一拳可以一次性破坏连续的 列。
- 更正式地讲,他可以选择一个 整数 ,满足 (包括两端),来破坏所有存在于(或部分存在于)第 到 列之间且尚未被摧毁的墙。
当墙的一部分被破坏时,该整面墙会因拳头的冲击而被完全摧毁。 (参见示例输入与输出 中的图示)
高桥至少需要出拳多少次才能摧毁所有墙壁?
以上为通义千问 qwen-max 翻译,仅供参考。
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.
Sample Input 1
3 3
1 2
4 7
5 9
Sample Output 1
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 .
Sample Input 2
3 3
1 2
4 7
4 9
Sample Output 2
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.
Sample Input 3
5 2
1 100
1 1000000000
101 1000
9982 44353
1000000000 1000000000
Sample Output 3
3
update @ 2024/3/10 10:03:32