AIM Tech Round 3 (Div. 2) B
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.
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 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的更多相关文章
- 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 ...
- AIM Tech Round 3 (Div. 2)
#include <iostream> using namespace std; ]; int main() { int n, b, d; cin >> n >> ...
- AIM Tech Round 3 (Div. 2) A B C D
虽然打的时候是深夜但是状态比较好 但还是犯了好多错误..加分场愣是打成了降分场 ABC都比较水 一会敲完去看D 很快的就想出了求0和1个数的办法 然后一直wa在第四组..快结束的时候B因为低级错误被h ...
- 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 ...
- 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 ...
- AIM Tech Round 3 (Div. 2) B 数学+贪心
http://codeforces.com/contest/709 题目大意:给一个一维的坐标轴,上面有n个点,我们刚开始在位置a,问,从a点开始走,走n-1个点所需要的最小路程. 思路:我们知道,如 ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- Ruby界面开发--wxRuby库TextCtrl相关问题
界面库官方教程:(1) 总的各种库函数讲解http://wxruby.rubyforge.org/doc/index.html (2)TextCtrl讲解 http://wxruby.rubyforg ...
- Java基础(47):插入排序的Java封装(含原理,可运行,哨兵位的理解见VisualGo上面的动态分析)
直接插入排序(Straight Insertion Sorting)的基本思想:在要排序的一组数中,假设前面(n-1) [n>=2] 个数已经是排好顺序的,现在要把第n个数插到前面的有序数中,使 ...
- mongo自带测试
benchrun > res = benchRun({ ops:[{ ns:"test.foo", op:"insert", doc:{y:{,]}} } ...
- jquery中的json操作
$(function() { var json = [ { "id" : "1", "tagName" : "apple" ...
- 夺命雷公狗—angularjs—13—post参数的接收发送
我们强悍的angularjs为我们不仅仅提供了他的get接收方式,而且也有post的接收方式,我们现在做一个模拟接收后端传递过来的json的数据: <?php $arr = ['user'=&g ...
- 关于archlinux下的ralink5370网卡
驱动此网卡要使用 rt2800usb,rt2800lib 这两个模块 顺便说一下对模块进行操作的命令: rmmod 模块名 //为移除模块 insmod 模块所在路径 //为添加模块 查看网卡是否能被 ...
- NOIP199904求Cantor表
求Cantor表 题目描述 Description 现代数学的著名证明之一是Georg Cantor证明了有理数是可枚举的.他是用下面这一张表来证明这一命题的: 1/1 1/2 1/3 1/4 1/5 ...
- 161110、彻底征服 Spring AOP 之 实战篇
Spring AOP 实战 看了上面这么多的理论知识, 不知道大家有没有觉得枯燥哈. 不过不要急, 俗话说理论是实践的基础, 对 Spring AOP 有了基本的理论认识后, 我们来看一下下面几个具体 ...
- javascript 金额格式化
金额格式化 example: <!DOCTYPE html> <html> <head> <script src="http://code.jque ...
- du -sh
评估文件空间利用率: [root@vm-xiluhua][/home]$ du -sh /home 409M /home --exclude选项,排除指定模式的文件的大小 [root@vm-xiluh ...