102701: [AtCoder]ABC270 B - Hammer
Description
Score : $200$ points
Problem Statement
Takahashi is at the origin of a number line. He wants to reach a goal at coordinate $X$.
There is a wall at coordinate $Y$, which Takahashi cannot go beyond at first.
However, after picking up a hammer at coordinate $Z$, he can destroy that wall and pass through.
Determine whether Takahashi can reach the goal. If he can, find the minimum total distance he needs to travel to do so.
Constraints
- $-1000 \leq X,Y,Z \leq 1000$
- $X$, $Y$, and $Z$ are distinct, and none of them is $0$.
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
$X$ $Y$ $Z$
Output
If Takahashi can reach the goal, print the minimum total distance he needs to travel to do so. If he cannot, print -1
instead.
Sample Input 1
10 -10 1
Sample Output 1
10
Takahashi can go straight to the goal.
Sample Input 2
20 10 -10
Sample Output 2
40
The goal is beyond the wall. He can get there by first picking up the hammer and then destroying the wall.
Sample Input 3
100 1 1000
Sample Output 3
-1
Input
题意翻译
高桥君在一条数轴的原点,想去坐标为 $X$ 处。 但坐标为 $Y$ 处有墙,无法通过。 坐标为 $Z$ 处有一把锤子,可以用它打碎墙以通过。 问:给出 $X,Y,Z$ ,则高桥君要达成目的所需的最短移动距离是多少?如果不能到达,输出 $-1$ 。 保证:$-1000 \leq X,Y,Z\leq1000$ ,且 $XYZ\neq0$ 。$X,Y,Z$ 互不相同且均为整数。Output
分数:200分
问题描述
高桥在数轴的原点处。他想到达坐标为$X$的目标。
在坐标为$Y$处有一堵墙,高桥起初不能通过。然而,在坐标为$Z$处拿到锤子后,他可以摧毁这堵墙并穿过去。
判断高桥是否能到达目标。如果能,找出他需要走的最短总距离。
限制条件
- $-1000 \leq X,Y,Z \leq 1000$
- $X$、$Y$和$Z$各不相同,且都不为$0$。
- 输入中的所有值都是整数。
输入
输入从标准输入以以下格式给出:
$X$ $Y$ $Z$
输出
如果高桥能到达目标,输出他需要走的最短总距离。如果不能,输出-1
代替。
样例输入1
10 -10 1
样例输出1
10
高桥可以直接到达目标。
样例输入2
20 10 -10
样例输出2
40
目标在墙的另一边。他可以先拿到锤子,然后摧毁墙到达目标。
样例输入3
100 1 1000
样例输出3
-1