A. Ropewalkers

题目链接:http://codeforces.com/contest/1185/problem/A

题目:

Polycarp decided to relax on his weekend and visited to the performance of famous ropewalkers: Agafon, Boniface and Konrad.

The rope is straight and infinite in both directions. At the beginning of the performance, Agafon, Boniface and Konrad are located in positions a, b and c respectively. At the end of the performance, the distance between each pair of ropewalkers was at least d.

Ropewalkers can walk on the rope. In one second, only one ropewalker can change his position. Every ropewalker can change his position exactly by 1
(i. e. shift by 1 to the left or right direction on the rope). Agafon, Boniface and Konrad can not move at the same time (Only one of them can move at each moment). Ropewalkers can be at the same positions at the same time and can "walk past each other".

You should find the minimum duration (in seconds) of the performance. In other words, find the minimum number of seconds needed so that the distance between each pair of ropewalkers can be greater or equal to d.

Ropewalkers can walk to negative coordinates, due to the rope is infinite to both sides.
Input

The only line of the input contains four integers a, b, c, d (1≤a,b,c,d≤109). It is possible that any two (or all three) ropewalkers are in the same position at the beginning of the performance.
Output

Output

one integer — the minimum duration (in seconds) of the performance.
Examples
Input

5 2 6 3

Output

2

Input

3 1 5 6

Output

8

Input

8 3 3 2

Output
2
Input

2 3 10 4

Output

3

Note

In the first example: in the first two seconds Konrad moves for 2 positions to the right (to the position 8
), while Agafon and Boniface stay at their positions. Thus, the distance between Agafon and Boniface will be |5−2|=3, the distance between Boniface and Konrad will be |2−8|=6 and the distance between Agafon and Konrad will be |5−8|=3. Therefore, all three pairwise distances will be at least d=3, so the performance could be finished within 2 seconds.

题意:

三个数a,b,c,在一坐标轴上,使得相邻的两个数之差大于等于d,求变换步数。

思路:

先三个数排序,计算相邻两个数的差值和d的差值即可

#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
const int maxn=2e5+;
int main()
{
int a,b,c,d;
while(cin>>a>>b>>c>>d)
{
int result=;
int _max=max(max(a,b),c);
int _min=min(min(a,b),c);
int _mid=a+b+c-_max-_min;
int _1=_mid-_min;
int _2=_max-_mid;
if(_1<d)
result+=(d-_1);
if(_2<d)
result+=(d-_2);
cout<<result<<endl;
}
return ;
}

Codeforces Round #568 (Div. 2)A的更多相关文章

  1. Codeforces Round #568 (Div. 2)B

    B. Email from Polycarp 题目链接:http://codeforces.com/contest/1185/problem/B 题目: Methodius received an e ...

  2. codeforces Round #568(Div.2)A B C

    有点菜,只写出了三道.活不多说,上题开干. A. Ropewalkers Polycarp decided to relax on his weekend and visited to the per ...

  3. Codeforces Round #568 (Div. 2) D. Extra Element

    链接: https://codeforces.com/contest/1185/problem/D 题意: A sequence a1,a2,-,ak is called an arithmetic ...

  4. Codeforces Round #568 (Div. 2) C2. Exam in BerSU (hard version)

    链接: https://codeforces.com/contest/1185/problem/C2 题意: The only difference between easy and hard ver ...

  5. Codeforces Round #568 (Div. 2) B. Email from Polycarp

    链接: https://codeforces.com/contest/1185/problem/B 题意: Methodius received an email from his friend Po ...

  6. Codeforces Round #568 (Div. 2) A.Ropewalkers

    链接: https://codeforces.com/contest/1185/problem/A 题意: Polycarp decided to relax on his weekend and v ...

  7. Codeforces Round #568 (Div. 2) G1. Playlist for Polycarp (easy version) (状压dp)

    题目:http://codeforces.com/contest/1185/problem/G1 题意:给你n给选项,每个选项有个类型和价值,让你选择一个序列,价值和为m,要求连续的不能有两个相同的类 ...

  8. Codeforces Round #568 Div. 2

    没有找到这场div3被改成div2的理由. A:签到. #include<bits/stdc++.h> using namespace std; #define ll long long ...

  9. Codeforces Round #568 (Div. 2) G2. Playlist for Polycarp (hard version)

    因为不会打公式,随意就先将就一下? #include<cstdio> #include<algorithm> #include<iostream> #include ...

随机推荐

  1. wpf 自定义窗口,最大化时覆盖任务栏解决方案

    原文:wpf 自定义窗口,最大化时覆盖任务栏解决方案 相信很多人使用wpf时会选择自定义美观的窗口,因此会设置WindowStyle="None" 取消自带的标题栏.但这样使用 W ...

  2. 《Linux Device Drivers》第十一章 核心数据类型——note

    基本介绍 因为Linux多平台特性,不管是哪一个重要驱动力应该是便携 与内核代码相关的核心问题应该是访问的同时是数据项的已知长度.能力和利用不同的处理器 内核使用的数据类型主要分为三类 类似int这种 ...

  3. CefSharp For WPF自定义右键菜单栏

    原文:CefSharp For WPF自定义右键菜单栏 初始化 <!--浏览器--> <cefSharpWPF:ChromiumWebBrowser Name="webBr ...

  4. eclise配置tomcat出现服务Tomcat version 6.0 only supports J2EE 1.2, 1.3, 1.4 and Java EE 5 Web modules

    当部署项目Tomcat version 6.0 only supports J2EE 1.2, 1.3, 1.4, 1.5 and Java EE 5 Web modules错;解决方案,如下面: 空 ...

  5. docker include not found: networks

    启动clickhouse的docker镜像时,出现了以下错误 include not found: networks google之后发现是因为可能不支持ipv6导致的解决方法 就是通过设置 /etc ...

  6. 简单说说Delphi中线程的释放

    线程的释放方式有两种:一种是线程在运行完成后自动释放,一种是手动释放. 无论是那种释放,都应该在线程停止后进行释放. 然而线程的停止也有两种情况:一种是不需要设置标志位,直接完成:一种是由于execu ...

  7. SQL 修改主键约束

    原文:SQL 修改主键约束 今天在学习数据库的时候遇到一个关于如何修改主键约束的问题,抄录下来以供备用. --修改主键约束用SQL --获取主键约束名字 declare @csname varchar ...

  8. RDLC报表 分组排序设置

    原文:RDLC分组排序设置 RDLC中反复设置表的排序字段,设置数据源排序后绑定均无效. 需要在分组字段右击组属性中,修改对应的排序字段,即可.

  9. JVM的几个介绍

    关于jvm内存的几点 jvm在运行时分为方法区(Method Area) .虚拟机栈(VM Stack).本地方法栈(Native Method Stack).堆 (Heap).程序计数器 (Prog ...

  10. ExtJS 折线图趟过的坑

    问题: 1.根据条件检索后绘制折线图,之前的坐标没有清除如图 解决方案: 在绘制之前,清空坐票: leftLine.surface.removeAll(); leftLine.redraw(false ...