B - Toll Gates 解説 /

実行時間制限: 2 sec / メモリ制限: 256 MB

配点 : 200200

問題文

N+1N + 1 個のマスが左右に一列に並んでいます. これらのマスには,左のマスから順に 0,1,...,N0, 1, ..., N の番号が付けられています.

あなたは,最初マス XX にいます. 隣り合うマスの間は自由に移動することができ,マス 00 またはマス NN にたどり着くとゴールすることができます. ただし,i=1,2,...,Mi = 1, 2, ..., M について,マス AiA_i には料金所があり,そのためマス AiA_i に移動してくる際には 11 のコストがかかります. なお,マス 00,マス XX,マス NN には料金所がないことが保証されます.

ゴールするまでにかかるコストの最小値を求めてください.

制約

  • 1N1001 \leq N \leq 100
  • 1M1001 \leq M \leq 100
  • 1XN11 \leq X \leq N - 1
  • 1A1<A2<...<AMN11 \leq A_1 < A_2 < ... < A_M \leq N - 1
  • AiXA_i \neq X
  • 入力はすべて整数

入力

入力は以下の形式で標準入力から与えられる。

NN MM XX
A1A_1 A2A_2 ...... AMA_M

出力

ゴールするまでにかかるコストの最小値を出力せよ.


入力例 1Copy

Copy
5 3 3
1 2 4

出力例 1Copy

Copy
1

次のようにするのが最適です.

  • まず,マス 33 から,マス 44 へ移動する.このとき,マス 44 には料金所があるので,コスト 11 がかかる.
  • 次に,マス 44 から,マス 55 へ移動する.このときはコストはかからない.
  • マス 55 に到着したので,ゴールする.

このようにすると,コストは合計で 11 になります.


入力例 2Copy

Copy
7 3 2
4 5 6

出力例 2Copy

Copy
0

まったくコストがかからないこともあります.


入力例 3Copy

Copy
10 7 5
1 2 3 4 6 8 9

出力例 3Copy

Copy
3

Score : 200200 points

Problem Statement

There are N+1N + 1 squares arranged in a row, numbered 0,1,...,N0, 1, ..., N from left to right.

Initially, you are in Square XX. You can freely travel between adjacent squares. Your goal is to reach Square 00 or Square NN. However, for each i=1,2,...,Mi = 1, 2, ..., M, there is a toll gate in Square AiA_i, and traveling to Square AiA_i incurs a cost of 11. It is guaranteed that there is no toll gate in Square 00, Square XX and Square NN.

Find the minimum cost incurred before reaching the goal.

Constraints

  • 1N1001 \leq N \leq 100
  • 1M1001 \leq M \leq 100
  • 1XN11 \leq X \leq N - 1
  • 1A1<A2<...<AMN1 \leq A_1 < A_2 < ... < A_M \leq N
  • AiXA_i \neq X
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

NN MM XX
A1A_1 A2A_2 ...... AMA_M

Output

Print the minimum cost incurred before reaching the goal.


Sample Input 1Copy

Copy
5 3 3
1 2 4

Sample Output 1Copy

Copy
1

The optimal solution is as follows:

  • First, travel from Square 33 to Square 44. Here, there is a toll gate in Square 44, so the cost of 11 is incurred.
  • Then, travel from Square 44 to Square 55. This time, no cost is incurred.
  • Now, we are in Square 55 and we have reached the goal.

In this case, the total cost incurred is 11.


Sample Input 2Copy

Copy
7 3 2
4 5 6

Sample Output 2Copy

Copy
0

We may be able to reach the goal at no cost.


Sample Input 3Copy

Copy
10 7 5
1 2 3 4 6 8 9

Sample Output 3Copy

Copy
3


2025-03-14 (金)
13:11:17 +00:00