AIM Tech Round 3 (Div. 2) B

Posted 樱花落舞

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了AIM Tech Round 3 (Div. 2) B相关的知识,希望对你有一定的参考价值。

Description

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 is12 - 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个坐标,问 至少去n-1个地方的总共距离是多少

解法:先排序,去n-1个点,那么就是去1到n-1,或者是2到n,讨论一下就可以了,开始点先去1,再转身去n-1,或者先去n-1再转身去1(开始点先去2,再转身去n,或者先去n再转身去2)

#include<bits/stdc++.h>
using namespace std;
int a[100005],pos,n;
long long sum[100005];
struct P
{
    int ans,cot;
}He[100005];
bool cmd(P x,P y)
{
    return x.ans<y.ans;
}
int main()
{

    cin>>n>>pos;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
    }
    sort(a+1,a+n+1);
    if(n==1)
    {
        cout<<"0"<<endl;
    }
    else if(n==2)
    {
        cout<<min(abs(pos-a[1]),abs(pos-a[2]));
    }
    else
    {
        int pos1=abs(pos-a[1]);
        int pos2=abs(a[1]-a[n-1]);
        int pos3=abs(pos-a[n-1]);
        int pos4=abs(pos-a[2]);
        int pos5=abs(pos-a[n-1]);
        int pos6=abs(a[n]-a[2]);
        int pos7=abs(pos-a[n]);
        cout<<min(min(pos1+pos2,pos3+pos2),min(pos4+pos6,pos7+pos6));
    }
    return 0;
}

  

以上是关于AIM Tech Round 3 (Div. 2) B的主要内容,如果未能解决你的问题,请参考以下文章

AIM Tech Round 3 (Div. 2) E. Centroids

AIM Tech Round (Div. 2)

Codeforces_AIM Tech Round 3 (Div. 1)_A

codeforces708b// Recover the String //AIM Tech Round 3 (Div. 1)

Codeforces_AIM Tech Round 3 (Div. 1)_B

AIM Tech Round 5 (rated, Div. 1 + Div. 2)