Codeforces 709B 模拟
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.
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.
Print one integer — the minimum distance Vasya has to travel in order to visit at least n - 1 checkpoint.
3 10
1 7 12
7
2 0
11 -10
10
5 0
0 0 1000 0 0
0
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个标记最少需要的路程。
#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 模拟的更多相关文章
- CodeForces 709B Checkpoints 模拟
题目大意:给出n个点的坐标,和你当前的坐标,求走过n-1个点的最短路程. 题目思路:走过n-1个点,为了使路程更短,那么不走的点只可能第一个点或最后一个点.模拟就行了,比较恶心. #include&l ...
- CodeForces - 427B (模拟题)
Prison Transfer Time Limit: 1000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u Sub ...
- CodeForces - 404B(模拟题)
Marathon Time Limit: 1000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u Submit Sta ...
- Checkpoints codeforces 709B
http://codeforces.com/problemset/problem/709/B 题意:给出一条横向坐标轴,给出Vasya所在的坐标位置及其另外n个坐标.Vasya想要至少访问n-1个位置 ...
- codeforces 709B Checkpoints
题目链接:http://codeforces.com/problemset/problem/709/B 题目大意: 第一行给出两个数 n,x.第二行 输入 n 个数. 要求:从x位置遍历 n-1 个位 ...
- CodeForces - 404A(模拟题)
Valera and X Time Limit: 1000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u Submit ...
- Codeforces 390A( 模拟题)
Inna and Alarm Clock Time Limit: 1000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64 ...
- Codeforces 452D [模拟][贪心]
题意: 给你k件衣服处理,告诉你洗衣机烘干机折叠机的数量,和它们处理一件衣服的时间,要求一件衣服在洗完之后必须立刻烘干,烘干之后必须立刻折叠,问所需的最小时间. 思路: 1.按照时间模拟 2.若洗完的 ...
- CodeForces - 796B 模拟
思路:模拟移动即可,如果球落入洞中停止移动.注意:有可能第一个位置就是洞!! AC代码 #include <cstdio> #include <cmath> #include ...
随机推荐
- 编译器错误消息: CS0016: 未能写入输出文件“c:/Windows/Microsoft.NET/Framework/v4.0.50727/Temporary ASP.NET Files/root .... 拒绝访问。
此问题困扰良久,得终极解决方案 环境:windows 2008 server r2 + iis7 + .net framework4.5 解决:1. 错误信息中包含的目录“c:/Windows/Mic ...
- 显式锁(四)Lock的等待通知机制Condition
任意一个Java对象,都拥有一组监视器方法(定义在根类Object上),主要包括:wait( ).wait(long timeout).notify().notifyAll()方法:这些方法与关 ...
- jmeter测试20个QPS下的响应时间-设置QPS限制
添加--->定时器--->Constant Throughput Timer Constant Throughput Timer 的主要属性介绍: 名称:定时器的名称 Target thr ...
- python入门-字典
1 python是使用{}来表示字典 字典是一系列的键值对 alien_0={} 2 访问字典中的值 new_point = alien_0['point'] print("you just ...
- CENTOS 挂载ntfs移动硬盘
参考网址: http://www.it610.com/article/3368930.htm (较全)http://blog.51cto.com/ultrasql/1927672
- Bootstrap Table 中文文档(完整翻译版)
表格参数: 名称 标签 类型 默认 描述 - data-toggle String ‘table’ 不用写 JavaScript 直接启用表格. classes data-classes String ...
- IIS 更新EXE文件
IIS 更新EXE文件 MIME,add,文件扩展名带不带.都可以,会自动加上.的 文件扩展名:.exe MIME类型:application/octet-stream .ini文件
- sendkeys
1)为了指定单一键盘字符,必须按字符本身的键.例如,为了表示字母 A,可以用 "A" 作为 string.为了表示多个字符,就必须在字符后面直接加上另一个字符.例如,要表示 A.B ...
- 机器学习入门-主成分分析(PCA)
主成分分析: 用途:降维中最常用的一种方法 目标:提取有用的信息(基于方差的大小) 存在的问题:降维后的数据将失去原本的数据意义 向量的内积:A*B = |A|*|B|*cos(a) 如果|B| = ...
- UI5-文档-4.6-Modules
在SAPUI5中,资源通常被称为模块.在这一步中,我们将上次练习中的警报替换为sap.m库中的适当消息Toast.所需的模块可以异步加载. Preview A message toast displa ...