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 ...
随机推荐
- navicate for mysql mac 含注册机 亲测可用
百度网盘:https://pan.baidu.com/s/1hrXnRes
- python之集合操作
1.集合的增操作 add(item):增加至集合中 update(set): 若不存在,更新至集合中 2.集合的删操作(五种) pop(): 随机删除并返回值 remove(item): 删除va ...
- [Flutter] Image.File 加载图像时文件内容变化显示不变解决
在Flutter中,我们可以用下面的代码从文件中加载图像: Image.file(File(_fileName)); 这个时候,当_fileName这个文件名称和路径不变,文件内容变化时,Flutte ...
- 使用Larave5.6l提交POST请求出现The page has expired due to inactivity错误
使用Larave5.6l提交POST请求出现The page has expired due to inactivity错误 一般是由于没有添加 csrf造成的 在表单下面的 第一个行 添加如下代码即 ...
- SQLSERVER数据库迁移的方法
数据库迁移两种方案:https://www.cnblogs.com/mcgrady/p/7614491.html 方案一 1,先将源服务器上的数据库文件打包(包括mdf和ldf文件),并且复制到目标服 ...
- 31. Studio获取新的ID值方法
var fun = ABS_LOADBEAN("com.plug.FunctionHelper");var vid1 = fun.utilHelper.getNextID(&quo ...
- spring boot 整合 (全)
参考: https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples
- redis、memcache和mongodb各自的优缺点是什么
redis.memcahce 比较相似,但与 mongodb 完全不同,几乎没有可比性. 总的来说 redis/memcache 是基于内存的,讲究的是性能,多用作缓存层,比如说存放session.而 ...
- bootstrap做的导航
顶部导航:nav-tabs 左边导航:nav-list 响应式布局:div嵌套 ~ container.row.ol-lg-X 效果: 源码: <!DOCTYPE html> <ht ...
- VBA 浏览文件夹
Private Function SelectFolder() As String With Application.FileDialog(msoFileDialogFolderPick ...