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. UI design principles

    Master's conclusion: 1. fix a color pattern 2. fix the frames the UI will use 3. fix the subject tha ...

  2. csuoj 1395: Timebomb

    http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1395 1395: Timebomb Time Limit: 1 Sec  Memory Limit ...

  3. [转]MongoDB for Java】Java操作MongoDB

    原文地址: MongoDB for Java]Java操作MongoDB 开发环境: System:Windows IDE:eclipse.MyEclipse 8 Database:mongoDB 开 ...

  4. 解决无法连接到visual studio开发服务器的问题

    今天vs抽风,调试网站出现下图那样: 然后我开始百度搜索 "无法连接到visual studio开发服务器" 出现很多文章: 打开一篇问题,看了里面的内容, 那个解决办法也不是最好 ...

  5. js 获取选中的多选框

    前台html <span class="spbox"> <input type="checkbox" name="category& ...

  6. mongodb子文档查询

    --子文档分页 -- 测试数据 db.childTests.insert({ "_id" : 1, "item" : "ABC", &quo ...

  7. Script to compile invalid objects in DB

    REM: Script to compile invalid objects in DB after refreshing REM: REM:***************************** ...

  8. Java中的数组操作进阶

    package com.mi.array; import java.util.Arrays; /** * System.arraycopy(i, 0, j, 0, i.length);这种复制会覆盖目 ...

  9. jquery 实践总结

    Ready事件 对DOM操作之前需要监听页面加载进度,应当在页面加载完成之后再执行DOM编辑操作. $(document).ready(function(){ ... }); 或者 $(functio ...

  10. android 弹幕效果demo

    记得之前有位朋友在我的公众号里问过我,像直播的那种弹幕功能该如何实现?如今直播行业确实是非常火爆啊,大大小小的公司都要涉足一下直播的领域,用斗鱼的话来讲,现在就是千播之战.而弹幕则无疑是直播功能当中最 ...