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. 《opencv学习》 之 几何变换

    图像平移: 1.不改变图像大小 2.改变图像大小 编程按照目标图像的角度去编写 不改变大小的平移 1 void imageTranslation1(Mat& src, Mat& dst ...

  2. oracle10偶然性卡住登陆

    连接数据库异常:登陆数据库后以"conn /as sysdba"方式登陆正常,数据库轻载,无压力:于是检查数据库的监听器,输入"lsntctl services" ...

  3. 使用IDEA开发Activiti工作流

    首先安装cativiti插件,安装成功后重启IDEA 然后在文件夹右键选择 然后就可以画图了,但是画图之后,没有连接图标怎么办呢 把鼠标放在开头的那个图标上,此时光标的形状改变了. 拖到另一个上面,连 ...

  4. 机器学习入门-主成分分析(PCA)

    主成分分析: 用途:降维中最常用的一种方法 目标:提取有用的信息(基于方差的大小) 存在的问题:降维后的数据将失去原本的数据意义 向量的内积:A*B = |A|*|B|*cos(a) 如果|B| = ...

  5. select 实现多路复用IO的server_socket

    select 对程序进行同时检测,当发生响应时,数据被拷贝到内核区域,内核区通知用户程序来进行读取数据,内核区域并不知道是客户端连接,因此需要进行循环 server_socket 端 import s ...

  6. oracle惯用缩写的含义

    $ORACLE_HOME/bin下的utilities解释Binary              First Available        Description----------------- ...

  7. Oracle创建表语句(Create table)语法详解及示例

    创建表(Create table)语法详解1. ORACLE常用的字段类型ORACLE常用的字段类型有VARCHAR2 (size) 可变长度的字符串, 必须规定长度CHAR(size) 固定长度的字 ...

  8. java编写一个汽车类,有属性:品牌、型号、排量、速度,有方法:启动、加速、转弯、刹车、息火

    /* * 汽车实体类 * 类里面有属性和方法 */public class Car {    String  brand;   //汽车品牌    String modelNumber; //汽车型号 ...

  9. selenium中使用chromedriver备忘

    chromedriver是chrome浏览器的webdriver的一个实现.ChromeDriver是由Chrome开发团队来完成的因而ChromeDriver不包含在selenium包中,需要从Ch ...

  10. 【Java】JVM(五)、虚拟机类加载机制

    一.概念 类加载:虚拟机把类的数据从Class文件加载到内存中,并对数据进行校验,转化解析,和初始化,最终形成可以被虚拟机直接使用的Java类型. 二.加载时机 1.加载 加载阶段虚拟机完成的工作为: ...