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的更多相关文章

  1. codeforce AIM tech Round 4 div 2 B rectangles

    2017-08-25 15:32:14 writer:pprp 题目: B. Rectangles time limit per test 1 second memory limit per test ...

  2. AIM Tech Round 3 (Div. 2)

    #include <iostream> using namespace std; ]; int main() { int n, b, d; cin >> n >> ...

  3. AIM Tech Round 3 (Div. 2) A B C D

    虽然打的时候是深夜但是状态比较好 但还是犯了好多错误..加分场愣是打成了降分场 ABC都比较水 一会敲完去看D 很快的就想出了求0和1个数的办法 然后一直wa在第四组..快结束的时候B因为低级错误被h ...

  4. AIM Tech Round 3 (Div. 2) A

    Description Kolya is going to make fresh orange juice. He has n oranges of sizes a1, a2, ..., an. Ko ...

  5. AIM Tech Round 3 (Div. 2) (B C D E) (codeforces 709B 709C 709D 709E)

    rating又掉下去了.好不容易蓝了.... A..没读懂题,wa了好几次,明天问队友补上... B. Checkpoints 题意:一条直线上n个点x1,x2...xn,现在在位置a,求要经过任意n ...

  6. AIM Tech Round 3 (Div. 2) B 数学+贪心

    http://codeforces.com/contest/709 题目大意:给一个一维的坐标轴,上面有n个点,我们刚开始在位置a,问,从a点开始走,走n-1个点所需要的最小路程. 思路:我们知道,如 ...

  7. AIM Tech Round 3 (Div. 2)D. Recover the String(贪心+字符串)

    D. Recover the String time limit per test 1 second memory limit per test 256 megabytes input standar ...

  8. AIM Tech Round 4 (Div. 2)ABCD

    A. Diversity time limit per test 1 second memory limit per test 256 megabytes input standard input o ...

  9. AIM Tech Round 4 (Div. 2)(A,暴力,B,组合数,C,STL+排序)

    A. Diversity time limit per test:1 second memory limit per test:256 megabytes input:standard input o ...

随机推荐

  1. Python学习总结15:时间模块datetime & time & calendar (二)

    二 .datetime模块  1. datetime中常量 1)datetime.MINYEAR,表示datetime所能表示的最小年份,MINYEAR = 1. 2)datetime.MAXYEAR ...

  2. JS_01_入门学习

    01:编写js文件:JavaScript_01.js // JavaScript Document //1.利用JavaScript来输出文字 <script type="text/j ...

  3. paper 86:行人检测资源(上)综述文献【转载,以后使用】

    行人检测具有极其广泛的应用:智能辅助驾驶,智能监控,行人分析以及智能机器人等领域.从2005年以来行人检测进入了一个快速的发展阶段,但是也存在很多问题还有待解决,主要还是在性能和速度方面还不能达到一个 ...

  4. VMware搭建 sql server2012集群加节点 KB953748

    --node4加入节点前 将共享磁盘挂载到node4,仅测试验证磁盘挂载,否则会导致整个集群磁盘offline切换 --node4 加入集群报错,无法访问计算机“node4” 0.public网卡勾选 ...

  5. [py]安装ipython

    系统:crunch bang11+python2.7.3 准备工具: sudo apt-get install python-pip python-dev build-essential 安装setu ...

  6. Three.js - 网页3D应用的普及已经指日可待了

    Three.js可以高效实现3D效果,真不赖.

  7. 三层架构与MVC的PK--ASP.NET MVC图解(一)

  8. UITableView(转)

    一.UITableView概述 UITableView继承自UIScrollView,可以表现为Plain和Grouped两种风格,分别如下图所示:          其中左边的是Plain风格的,右 ...

  9. android 中的几种目录

    1. context.getExternalFilesDir()     ==> /sdcard/Android/data/<package_name>/files/ 一般放一些长时 ...

  10. Linux设备驱动之semaphore机制【转】

    转自:http://blog.csdn.net/xiao229404041/article/details/7031776 Linux设备驱动之semaphore机制在Linux系统中,信号号是一种重 ...