CF709B Checkpoints 模拟
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.
4种情况求一下min即可;
- #include<iostream>
- #include<cstdio>
- #include<algorithm>
- #include<cstdlib>
- #include<cstring>
- #include<string>
- #include<cmath>
- #include<map>
- #include<set>
- #include<vector>
- #include<queue>
- #include<bitset>
- #include<ctime>
- #include<deque>
- #include<stack>
- #include<functional>
- #include<sstream>
- //#include<cctype>
- //#pragma GCC optimize(2)
- using namespace std;
- #define maxn 1000005
- #define inf 0x3f3f3f3f
- //#define INF 1e18
- #define rdint(x) scanf("%d",&x)
- #define rdllt(x) scanf("%lld",&x)
- #define rdult(x) scanf("%lu",&x)
- #define rdlf(x) scanf("%lf",&x)
- #define rdstr(x) scanf("%s",x)
- typedef long long ll;
- typedef unsigned long long ull;
- typedef unsigned int U;
- #define ms(x) memset((x),0,sizeof(x))
- const long long int mod = 1e9 + 7;
- #define Mod 1000000000
- #define sq(x) (x)*(x)
- #define eps 1e-3
- typedef pair<int, int> pii;
- #define pi acos(-1.0)
- const int N = 1005;
- #define REP(i,n) for(int i=0;i<(n);i++)
- typedef pair<int, int> pii;
- inline ll rd() {
- ll x = 0;
- char c = getchar();
- bool f = false;
- while (!isdigit(c)) {
- if (c == '-') f = true;
- c = getchar();
- }
- while (isdigit(c)) {
- x = (x << 1) + (x << 3) + (c ^ 48);
- c = getchar();
- }
- return f ? -x : x;
- }
- ll gcd(ll a, ll b) {
- return b == 0 ? a : gcd(b, a%b);
- }
- ll sqr(ll x) { return x * x; }
- /*ll ans;
- ll exgcd(ll a, ll b, ll &x, ll &y) {
- if (!b) {
- x = 1; y = 0; return a;
- }
- ans = exgcd(b, a%b, x, y);
- ll t = x; x = y; y = t - a / b * y;
- return ans;
- }
- */
- int n, a;
- ll x[maxn];
- int main()
- {
- //ios::sync_with_stdio(0);
- rdint(n); rdint(a);
- for (int i = 1; i <= n; i++)rdllt(x[i]);
- ll sum = 0;
- sort(x + 1, x + 1 + n);
- if (n == 1)cout << 0 << endl;
- else {
- sum = inf;
- sum = min(sum, abs(a - x[1]) + x[n - 1] - x[1]);
- sum = min(sum, abs(a - x[n - 1]) + x[n - 1] - x[1]);
- sum = min(sum, abs(a - x[n]) + x[n] - x[2]);
- sum = min(sum, abs(a - x[2]) + x[n] - x[2]);
- cout << sum << endl;
- }
- return 0;
- }
CF709B Checkpoints 模拟的更多相关文章
- CodeForces 709B Checkpoints 模拟
题目大意:给出n个点的坐标,和你当前的坐标,求走过n-1个点的最短路程. 题目思路:走过n-1个点,为了使路程更短,那么不走的点只可能第一个点或最后一个点.模拟就行了,比较恶心. #include&l ...
- 微软BI 之SSIS 系列 - 通过设置 CheckPoints 检查点来增强 SSIS Package 流程的重用性
开篇介绍 通常一个 ETL Package 是由多个控制流和数据流共同组成,有的时候 ETL 的步骤可能会比较多,整个流程执行下来的时间可能比较长.假设在 ETL Package 中包含5个Task, ...
- Capture the Flag(模拟)
Capture the Flag Time Limit: 2 Seconds Memory Limit: 65536 KB Special Judge In computer se ...
- Codeforces 709B 模拟
B. Checkpoints time limit per test:1 second memory limit per test:256 megabytes input:standard input ...
- Capture the Flag ZOJ - 3879(模拟题)
In computer security, Capture the Flag (CTF) is a computer security competition. CTF contests are us ...
- App开发:模拟服务器数据接口 - MockApi
为了方便app开发过程中,不受服务器接口的限制,便于客户端功能的快速测试,可以在客户端实现一个模拟服务器数据接口的MockApi模块.本篇文章就尝试为使用gradle的android项目设计实现Moc ...
- 故障重现, JAVA进程内存不够时突然挂掉模拟
背景,服务器上的一个JAVA服务进程突然挂掉,查看产生了崩溃日志,如下: # Set larger code cache with -XX:ReservedCodeCacheSize= # This ...
- Python 爬虫模拟登陆知乎
在之前写过一篇使用python爬虫爬取电影天堂资源的博客,重点是如何解析页面和提高爬虫的效率.由于电影天堂上的资源获取权限是所有人都一样的,所以不需要进行登录验证操作,写完那篇文章后又花了些时间研究了 ...
- HTML 事件(四) 模拟事件操作
本篇主要介绍HTML DOM中事件的模拟操作. 其他事件文章 1. HTML 事件(一) 事件的介绍 2. HTML 事件(二) 事件的注册与注销 3. HTML 事件(三) 事件流与事件委托 4. ...
随机推荐
- RabbitMQ 消息队列 安装及使用
RabbitMQ 消息队列安装: linux版本:CentOS 7 安装第一步:先关闭防火墙 1.Centos7.x关闭防火墙 [root@rabbitmq /]# systemctl stop fi ...
- hibernate 单元测试
单元测试 测试 dao service action package com.kaishengit.test; import org.hibernate.Session; import com.ka ...
- Socket中常见的几个转换函数(htonl,htons,ntohl,ntohs,inet_addr,inet_ntoa)
Socket中常见的几个转换函数(htonl,htons,ntohl,ntohs,inet_addr,inet_ntoa) htonl() htons() ntohl() ntohs()及inet_n ...
- CSS——position
position是指元素的定位方式,有:static.absolute.fixed.relative.inherit 5种. static 默认,布局排版方式按照HTML代码的顺序布局. absolu ...
- maven依赖scope配置项讲解
我们在使用Maven配置依赖项的时候,常常只会配置Maven的坐标以及版本信息就可以了,但我们看其他人的工程代码的时候常常会见到有个scope配置项,今天就来分别介绍下这个配置下几个类别的作用. &l ...
- tornado带签名的cookie原理
- Java构造函数中调用构造函数
在Java中,当为一个类创建了多个构造函数时,有时想在一个构造函数中调用另一个构造函数以减少代码量.这时可以使用this关键字来实现. 通常,当使用this关键字时,它意味着"这个对象&qu ...
- Python 网络爬虫 004 (编程) 如何编写一个网络爬虫,来下载(或叫:爬取)一个站点里的所有网页
爬取目标站点里所有的网页 使用的系统:Windows 10 64位 Python语言版本:Python 3.5.0 V 使用的编程Python的集成开发环境:PyCharm 2016 04 一 . 首 ...
- 算法Sedgewick第四版-第1章基础-1.4 Analysis of Algorithms-005计测试算法
1. package algorithms.analysis14; import algorithms.util.StdOut; import algorithms.util.StdRandom; / ...
- Luogu 2114 [NOI2014]起床困难综合症
还挺简单的. 发现这几个二进制运算并不会进位,所以我们从高到低按位贪心,一位一位计算贡献. 发现$2^{30}$刚好大于$1e9$,所以最多只要算29位. 首先算出一个全都是$0$的二进制数和一个全都 ...