

Time Limit: 2 sec / Memory Limit: 1024 MB
配点 : 点
問題文
行 列の格子状の区画に区切られた街に 枚の壁があり、 から までの番号が割り振られています。
壁 は上から 行目、左から 列目から 列目の範囲にあります。(入出力例 の図も参考にしてください。)
高橋君は 枚の壁をすべて破壊することにしました。
超人的な腕力を持つ高橋君は、 回のパンチで連続する 列にまとめてダメージを与えることができます。
- より厳密に言い換えると、 以上 以下の 整数 を選んで、 列目から 列目に (一部でも) 存在するすべての破壊されていない壁にパンチによるダメージを与えることができます。
壁は一部分でもダメージを受けると、パンチの衝撃波により全体が木っ端みじんに破壊されてしまいます。
(入出力例 の説明も参考にしてください。)
高橋君がすべての壁を破壊するには、少なくとも何回のパンチを放つ必要がありますか?
制約
- 入力はすべて整数である。
入力
入力は以下の形式で標準入力から与えられる。
出力
すべての壁を破壊するのに必要なパンチの最少回数を出力せよ。
入力例 1Copy
3 3 1 2 4 7 5 9
出力例 1Copy
2
入力例 に対応する壁の配置を図示したものが下図です。
たとえば次のようにパンチを放つことで、 回のパンチですべての壁を破壊することができます。(以下の説明では、 列目から 列目までの範囲を と表記します。)
- まず、 にパンチを放つ。 に存在する壁である壁 と壁 はダメージを受け、破壊される。
- 次に にパンチを放つ。 に存在する壁 はダメージを受け、破壊される。
また、次の方法でもすべての壁を 回のパンチで破壊することができます。
- まず、 にパンチを放ち、壁 と壁 を破壊する。
- 次に、 にパンチを放ち、壁 を破壊する。
入力例 2Copy
3 3 1 2 4 7 4 9
出力例 2Copy
1
入出力例 と比べると、壁 の範囲が から に変わりました。
この場合は にパンチを放つことで 回ですべての壁を破壊できます。
入力例 3Copy
5 2 1 100 1 1000000000 101 1000 9982 44353 1000000000 1000000000
出力例 3Copy
3
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.
Sample Input 1Copy
3 3 1 2 4 7 5 9
Sample Output 1Copy
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 2Copy
3 3 1 2 4 7 4 9
Sample Output 2Copy
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 3Copy
5 2 1 100 1 1000000000 101 1000 9982 44353 1000000000 1000000000
Sample Output 3Copy
3