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. 压缩 & 解压缩 命令汇总:tar、zip & unzip、

    1. tar命令详解     格式:tar [-cxtzjvfpPN] 文件与目录 -c: 建立压缩档案 -x:解压 -t:查看内容 -r:向压缩归档文件末尾追加文件 -u:更新原压缩包中的文件 这五 ...

  2. 关于.Net Remoting 和 Web Servcie的比较

    参照文献 http://www.cnblogs.com/shinehouse/articles/3001955.html http://www.cnblogs.com/paper/archive/20 ...

  3. 解析xml文件

    package com.ss1.xml; import java.io.File; import java.io.FileOutputStream; import java.io.IOExceptio ...

  4. MySQL的基本函数

    charset(str) //返回字串字符集 mysql> select charset('demacia'); +--------------------+ | charset('demaci ...

  5. JSP-06-使用JDBC操作数据库

    6.1 使用JDBC查询数据 [链接oracle数据库] 具体操作: 1)  将odbc文件拷贝到项目中 Odbc文件所在目录: oracle安装目录下- product – db_1 – jdbc  ...

  6. linux的vim按了ctrl+s之后假死的解决办法

    习惯了很多软件的保存的快捷键,经常在vim中按下ctrl+s,然后就发现vim不响应了,之间一直采用kill的方式解决,近来搜了一下,是这样子的: 这时的vim并没有死掉,只是vim不再向终端输出东西 ...

  7. centos的网路配置文件的位置

    在这个路径下:/etc/sysconfig/network-scripts/ 每个网卡有相应的配置文件

  8. JS中数组的操作

    1.数组的创建 var arrayObj = new Array(); //创建一个数组 var arrayObj = new Array([size]); //创建一个数组并指定长度,注意不是上限, ...

  9. 在线白板,基于socket.io的多人在线协作工具

    首发:个人博客,更新&纠错&回复 是昨天这篇博文留的尾巴,socket.io库的使用练习,成品地址在这里. 代码已经上传到github,传送门.可以开俩浏览器看效果. 现实意义是俩人在 ...

  10. SQL SERVER2000中订阅与发布的具体操作

    同步过程 一.准备工作,如果完成则可跳过. 1.内网DB服务器作为发布服务器,外网DB服务器作为订阅服务器. 发布服务器和订阅服务器上分别创建Windows用户jl,密码jl,隶属于administr ...