D - 隣接スワップの廊下 解説 /

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 400

問題文

整数座標がついたマスが一列に無限に並んだ廊下があります。この廊下には高橋君と青木君の2人だけがおり、それぞれ異なるマスに立っています。はじめ、高橋君はマス A に、青木君はマス B にいます。

最終的に、高橋君をマス C に、青木君をマス D に移動させたいです。

あなたは次の操作を好きな回数(0回でもよい)行えます。

  • 任意の整数 x1 つ選び、マス x とマス x + 1 にいる人を交換する。具体的には、マス x とマス x+1 の両方に人がいる場合は2人の位置が交換され、一方のマスにだけ人がいる場合はその人がもう一方のマスへ移動し、どちらのマスにも人がいない場合は何も起こらない。

この操作は隣接する2マスの間での交換であるため、1つのマスに2人が同時に存在することは決して起こりません。

与えられる入力に対して、目標の配置は必ず達成可能であることが証明できます。目標の配置を達成するために必要な最小の操作回数を求めてください。

制約

  • -10^{18} \leq A, B, C, D \leq 10^{18}
  • A \neq B
  • C \neq D
  • 入力はすべて整数である
  • この制約下で、答えは符号付き 64 bit 整数型に収まる

入力

A B C D

1 行目には、高橋君の初期位置を表す整数 A 、青木君の初期位置を表す整数 B 、高橋君の目標位置を表す整数 C 、青木君の目標位置を表す整数 D が、スペース区切りで与えられる。

出力

目標の配置を達成するために必要な最小の操作回数を整数で出力せよ。


入力例 1

0 2 1 3

出力例 1

2

入力例 2

0 1 1 0

出力例 2

1

入力例 3

-10 20 15 -5

出力例 3

49

入力例 4

-1000000000 2000000000 3000000000 -4000000000

出力例 4

9999999999

入力例 5

-1000000000000000000 1000000000000000000 999999999999999999 -999999999999999999

出力例 5

3999999999999999997

Score : 400 pts

Problem Statement

There is a corridor consisting of an infinite row of cells labeled with integer coordinates. Only two people are in this corridor: Takahashi and Aoki, each standing on a different cell. Initially, Takahashi is on cell A and Aoki is on cell B.

The goal is to move Takahashi to cell C and Aoki to cell D.

You can perform the following operation any number of times (possibly zero):

  • Choose any integer x, and swap the people on cell x and cell x + 1. Specifically, if both cells x and x+1 have a person, the two people exchange positions; if only one of the cells has a person, that person moves to the other cell; if neither cell has a person, nothing happens.

Since this operation is a swap between two adjacent cells, it is never possible for two people to occupy the same cell simultaneously.

It can be proven that the goal configuration is always achievable for the given inputs. Find the minimum number of operations required to achieve the goal configuration.

Constraints

  • -10^{18} \leq A, B, C, D \leq 10^{18}
  • A \neq B
  • C \neq D
  • All inputs are integers
  • Under these constraints, the answer fits in a signed 64-bit integer type

Input

A B C D

The first line contains integers A, B, C, D separated by spaces, representing Takahashi's initial position, Aoki's initial position, Takahashi's goal position, and Aoki's goal position, respectively.

Output

Print the minimum number of operations required to achieve the goal configuration as an integer.


Sample Input 1

0 2 1 3

Sample Output 1

2

Sample Input 2

0 1 1 0

Sample Output 2

1

Sample Input 3

-10 20 15 -5

Sample Output 3

49

Sample Input 4

-1000000000 2000000000 3000000000 -4000000000

Sample Output 4

9999999999

Sample Input 5

-1000000000000000000 1000000000000000000 999999999999999999 -999999999999999999

Sample Output 5

3999999999999999997