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 101 7 12
output
7
input
2 011 -10
output
10
input
5 00 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; }