CF709B.Checkpoints

普及/提高-

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Vasya takes part in the orienteering competition. There are n checkpoints located along the line at coordinates _x_1, _x_2, ..., x__n. Vasya starts at the point with coordinate a. His goal is to visit at least n - 1 checkpoint in order to finish the competition. Participant are allowed to visit checkpoints in arbitrary order.

Vasya wants to pick such checkpoints and the order of visiting them that the total distance travelled is minimized. He asks you to calculate this minimum possible value.

瓦西娅参加定向越野比赛。共有 nn 个检查点,沿一条直线分布,其坐标分别为 x1, x2, …, xnx_1,\,x_2,\,\dots,\,x_n。瓦西娅从坐标为 aa 的点出发。他的目标是访问至少 n−1n-1 个检查点,以完成比赛。参赛者可以按任意顺序访问检查点。

瓦西娅希望选择一组检查点以及访问它们的顺序,使得所经过的总路程最短。他请你计算这个可能的最小值。

输入格式

The first line of the input contains two integers n and a (1 ≤ n ≤ 100 000,  - 1 000 000 ≤ a ≤ 1 000 000) — the number of checkpoints and Vasya's starting position respectively.

The second line contains n integers _x_1, _x_2, ..., x__n ( - 1 000 000 ≤ x__i ≤ 1 000 000) — coordinates of the checkpoints.

输入的第一行包含两个整数 nn 和 aa(1≤n≤100 0001 \leq n \leq 100\,000,−1 000 000≤a≤1 000 000-1\,000\,000 \leq a \leq 1\,000\,000)—— 分别表示检查点的数量以及瓦西亚的起始位置。

第二行包含 nn 个整数 x1, x2, …, xnx_1,\,x_2,\,\dots,\,x_n(−1 000 000≤xi≤1 000 000-1\,000\,000 \leq x_i \leq 1\,000\,000)—— 表示各个检查点的坐标。

输出格式

Print one integer — the minimum distance Vasya has to travel in order to visit at least n - 1 checkpoint.

输出一个整数——Vasya 为访问至少 n−1n-1 个检查点所需经过的最短距离。

输入输出样例

  • 输入#1

    3 10
    1 7 12

    输出#1

    7
  • 输入#2

    2 0
    11 -10

    输出#2

    10
  • 输入#3

    5 0
    0 0 1000 0 0

    输出#3

    0

说明/提示

In the first sample Vasya has to visit at least two checkpoints. The optimal way to achieve this is the walk to the third checkpoints (distance is 12 - 10 = 2) and then proceed to the second one (distance is 12 - 7 = 5). The total distance is equal to 2 + 5 = 7.

In the second sample it's enough to visit only one checkpoint so Vasya should just walk to the point  - 10.

在第一个样例中,Vasya 至少需要访问两个检查点。达成此目标的最优路径是:先走到第三个检查点(距离为 12−10=212 - 10 = 2),再前往第二个检查点(距离为 12−7=512 - 7 = 5)。总距离为 2+5=72 + 5 = 7。

在第二个样例中,仅需访问一个检查点,因此 Vasya 只需走到位置 −10-10 即可。

输入解题思路,AI测评打分。不知道怎么写?

首页