题目链接:http://codeforces.com/contest/709/problem/B
题意:数轴上有n个点和一个a点,一个人从a点出发,要到n个点中的n-1个点去。问怎么走这个人的距离最少,求最少距离。
排序后有四种走法,包括了所有可能的解。1~n-1,n-1~1,2~n,n~2。trick是n=1的时候应该输出0。
1 /* 2 ━━━━━┒ギリギリ♂ eye! 3 ┓┏┓┏┓┃キリキリ♂ mind! 4 ┛┗┛┗┛┃\○/ 5 ┓┏┓┏┓┃ / 6 ┛┗┛┗┛┃ノ) 7 ┓┏┓┏┓┃ 8 ┛┗┛┗┛┃ 9 ┓┏┓┏┓┃ 10 ┛┗┛┗┛┃ 11 ┓┏┓┏┓┃ 12 ┛┗┛┗┛┃ 13 ┓┏┓┏┓┃ 14 ┃┃┃┃┃┃ 15 ┻┻┻┻┻┻ 16 */ 17 #include <algorithm> 18 #include <iostream> 19 #include <iomanip> 20 #include <cstring> 21 #include <climits> 22 #include <complex> 23 #include <fstream> 24 #include <cassert> 25 #include <cstdio> 26 #include <bitset> 27 #include <vector> 28 #include <deque> 29 #include <queue> 30 #include <stack> 31 #include <ctime> 32 #include <set> 33 #include <map> 34 #include <cmath> 35 using namespace std; 36 #define fr first 37 #define sc second 38 #define cl clear 39 #define BUG puts("here!!!") 40 #define W(a) while(a--) 41 #define pb(a) push_back(a) 42 #define Rint(a) scanf("%d", &a) 43 #define Rs(a) scanf("%s", a) 44 #define Cin(a) cin >> a 45 #define FRead() freopen("in", "r", stdin) 46 #define FWrite() freopen("out", "w", stdout) 47 #define Rep(i, len) for(int i = 0; i < (len); i++) 48 #define For(i, a, len) for(int i = (a); i < (len); i++) 49 #define Cls(a) memset((a), 0, sizeof(a)) 50 #define Clr(a, x) memset((a), (x), sizeof(a)) 51 #define Full(a) memset((a), 0x7f7f7f, sizeof(a)) 52 #define lrt rt << 1 53 #define rrt rt << 1 | 1 54 #define pi 3.14159265359 55 #define RT return 56 #define lowbit(x) x & (-x) 57 #define onecnt(x) __builtin_popcount(x) 58 typedef long long LL; 59 typedef long double LD; 60 typedef unsigned long long ULL; 61 typedef pair<int, int> pii; 62 typedef pair<string, int> psi; 63 typedef pair<LL, LL> pll; 64 typedef map<string, int> msi; 65 typedef vector<int> vi; 66 typedef vector<LL> vl; 67 typedef vector<vl> vvl; 68 typedef vector<bool> vb; 69 70 const int maxn = 100100; 71 int n, a; 72 LL x[maxn]; 73 74 int main() { 75 // FRead(); 76 while(~scanf("%d%d",&n,&a)) { 77 For(i, 1, n+1) cin >> x[i]; 78 if(n == 1) { 79 puts("0"); 80 continue; 81 } 82 sort(x+1, x+n+1); 83 LL ret = 0x7f7f7f7f7f7fLL; 84 ret = min(ret, abs(x[1]-a)+x[n-1]-x[1]); 85 ret = min(ret, abs(x[2]-a)+x[n]-x[2]); 86 ret = min(ret, abs(x[n]-a)+x[n]-x[2]); 87 ret = min(ret, abs(x[n-1]-a)+x[n-1]-x[1]); 88 cout << ret << endl; 89 } 90 }