B. Checkpoints
time limit per test:1 second
memory limit per test:256 megabytes
input:standard input
output:standard output

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 is 12 - 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.

题目连接:http://codeforces.com/contest/709/problem/B


题意:一条直线上面有n个标记,一个起点a。从起点开始出发最少经过n-1个标记最少需要的路程。
思路:模拟。路程最少,所以只需要经过n-1个点。a的右边经过cou个点,则坐标经过n-1-cou个点。
代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int x[];
int l[],r[];
int main()
{
int i,n,a;
scanf("%d%d",&n,&a);
int cou=;
for(i=; i<=n; i++)
{
scanf("%d",&x[i]);
if(x[i]<=a) cou++;
}
int cou1=cou;
sort(x+,x+n+);
for(i=; i<=n; i++)
{
if(x[i]<=a) l[cou]=x[i],cou--;
else r[++cou]=x[i];
}
l[]=r[]=a;
int cou2=cou;
int ans=;
for(i=; i<=cou2; i++)
{
if(i>n-) break;
if(n--i>cou1) continue;
int sign;
if(i==) sign=a-l[n-];
else if(i==n-) sign=r[n-]-a;
else
{
if(r[i]-a<=a-l[n--i]) sign=r[i]-a+r[i]-l[n--i];
else sign=a-l[n--i]+r[i]-l[n--i];
}
if(sign<ans) ans=sign;
}
cout<<ans<<endl;
return ;
}

Codeforces 709B 模拟的更多相关文章

  1. CodeForces 709B Checkpoints 模拟

    题目大意:给出n个点的坐标,和你当前的坐标,求走过n-1个点的最短路程. 题目思路:走过n-1个点,为了使路程更短,那么不走的点只可能第一个点或最后一个点.模拟就行了,比较恶心. #include&l ...

  2. CodeForces - 427B (模拟题)

    Prison Transfer Time Limit: 1000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u Sub ...

  3. CodeForces - 404B(模拟题)

    Marathon Time Limit: 1000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u Submit Sta ...

  4. Checkpoints codeforces 709B

    http://codeforces.com/problemset/problem/709/B 题意:给出一条横向坐标轴,给出Vasya所在的坐标位置及其另外n个坐标.Vasya想要至少访问n-1个位置 ...

  5. codeforces 709B Checkpoints

    题目链接:http://codeforces.com/problemset/problem/709/B 题目大意: 第一行给出两个数 n,x.第二行 输入 n 个数. 要求:从x位置遍历 n-1 个位 ...

  6. CodeForces - 404A(模拟题)

    Valera and X Time Limit: 1000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u Submit ...

  7. Codeforces 390A( 模拟题)

    Inna and Alarm Clock Time Limit: 1000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64 ...

  8. Codeforces 452D [模拟][贪心]

    题意: 给你k件衣服处理,告诉你洗衣机烘干机折叠机的数量,和它们处理一件衣服的时间,要求一件衣服在洗完之后必须立刻烘干,烘干之后必须立刻折叠,问所需的最小时间. 思路: 1.按照时间模拟 2.若洗完的 ...

  9. CodeForces - 796B 模拟

    思路:模拟移动即可,如果球落入洞中停止移动.注意:有可能第一个位置就是洞!! AC代码 #include <cstdio> #include <cmath> #include ...

随机推荐

  1. zabbix监控windows磁盘空间

    监控windows磁盘空间,不是百分比. 当windows系统添加相应的windows模板后,会自动生成检测系统空间的监控项,在应用集(Filessystem)里面,Free disk space o ...

  2. rhel7配置ELK过程

    参考网站:https://www.cnblogs.com/hongdada/p/7887455.html https://my.oschina.net/codingcloud/blog/1615013 ...

  3. JAVA servlet 上传文件(commons-fileupload, commons-io)

    <1>获取二进制文件流并输出 InputStream inputStream = request.getInputStream(); BufferedReader reader = new ...

  4. 【转】Maven中-DskipTests和-Dmaven.test.skip=true的区别

    主要区别是:是否编译测试类 -DskipTests:编译测试类,但不运行 -Dmaven.test.skip=true:不编译.不运行 转自 http://zephiruswt.blog.51cto. ...

  5. word 标题映射错乱

    关闭Document Map,退出word 再次打开

  6. svn异常处理

    TortoiseSVN 为客户端,SUBVersion为服务器端. 1.安装的tortoiseSVN不在鼠标右键菜单栏 出现这种原因是电脑的系统和svn不符,即电脑是64位系统,而svn是32位的. ...

  7. 基于OpenGL编写一个简易的2D渲染框架-02 搭建OpenGL环境

    由于没有使用GLFW库,接下来得费一番功夫. 阅读这篇文章前请看一下这个网页:https://learnopengl-cn.github.io/01%20Getting%20started/02%20 ...

  8. vue深入了解组件——动态组件&异步组件

    一.在动态组件上使用 keep-alive 我们之前曾经在一个多标签的界面中使用 is 特性来切换不同的组件: <component v-bind:is="currentTabComp ...

  9. uiview animation 卡一下

    原因:有个下载图片的地方在主线程执行,导致动画卡一下.

  10. tensorflow笔记之softmax_cross_enropy

    tf.nn.sparse_softmax_cross_entropy_with_logits() 当正确结果只有一个时,可以加速计算,比如MNIST数字识别,每张图片中仅包含一个数字,所以可以使用这个 ...