AIM Tech Round 3 (Div. 2) B. Checkpoints

B. Checkpoints

time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Vasya takes part in the orienteering competition. There are n checkpoints located along the line at coordinates x1, x2, …, xn. 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.
Input

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 x1, x2, …, xn ( - 1 000 000 ≤ xi ≤ 1 000 000) — coordinates of the checkpoints.
Output

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

3 10
1 7 12

Output

7

Input

2 0
11 -10

Output

10

Input

5 0
0 0 1000 0 0

Output

0

Note

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.

题意:在一条线上有 n 个点,然后给出初始位置,求要到达至少n1个点行走的最短距离。

思路:我们按照点所在坐标排序之后,不管初始状态处于什么位置,那一个不会到达的点必定会是所有点的最左或最右,这种情况下是最优解,所以,我们计算一下不经过最左边的点和不经过最右边的点的方案的最优取去最小即可。

ac代码:

/* ***********************************************
Author       : AnICoo1
Created Time : 2016-08-25-16.05 Thursday
File Name    : D:\MyCode\2016-8月\2016-8-25.cpp
LANGUAGE     : C++
Copyright  2016 clh All Rights Reserved
************************************************ */
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stack>
#include<set>
#include<map>
#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>
#define LL long long
#define ll __int64
#define mem(x,y) memset(x,(y),sizeof(x))
#define PI acos(-1)
#define gn (sqrt(5.0)+1)/2
#define eps 1e-8
using namespace std;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll powmod(ll a,ll b,ll MOD){ll ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}
double dpow(double a,ll b){double ans=1.0;while(b){if(b%2)ans=ans*a;a=a*a;b/=2;}return ans;}
const ll INF=1e9+10;
const int MAXN=1e6+10;
const int MOD=1e9+7;
//head
int a[MAXN];
int main()
{
    int n,loc;cin>>n>>loc;
    for(int i=0;i<n;i++) cin>>a[i];
    if(n==1)
    {
        printf("0\n");
        return 0;
    }
    sort(a,a+n);
    int dis1=min(abs(a[1]-loc),abs(a[n-1]-loc))+abs(a[1]-a[n-1]);
    int dis2=min(abs(a[0]-loc),abs(a[n-2]-loc))+abs(a[0]-a[n-2]);
    printf("%d\n",min(dis1,dis2));
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值