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. Linux性能测试 KSysguard工具

    KDE System Guard (KSysguard)是KDE的任务管理和性能监控工具.它采用client/server架构,可以监控本机也可以监控远端主机. KDE System Guard默认的 ...

  2. Haroopad 安装到 Mac OSX

    打开Terminal 控制台 粘贴运行代码安装换cask: ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/ ...

  3. MyBatis Generator 详解 专题

    idea中有plugin可提高效率: http://www.henryxi.com/use-idea-mybatis-plugin-generate-mapper-files eg: <?xml ...

  4. 批量删除C和C++注释

    使用Python语言 ,实现批量删除C/C++类型注释1.目前支持去掉.h .hpp .c .cpp .java 这些以//或/**/为注释符的源文件 2.支持递归遍历目录 3.当前版本为Python ...

  5. DataTable转换为Entity(反射&&泛型)

    public static IEnumerable<T> Parse<T>(IEnumerable<DataRow> rows) where T : class, ...

  6. 读BeautifulSoup官方文档之与bs有关的对象和属性(2)

    上一节说到tag, 这里接着讲, tag有个属性叫做string, tag.string其实就是我们要掌握的四个对象中的第二个 ---- NavigableString,  它代表的是该tag内的te ...

  7. 【C#】WindowsAPICodePack-Shell使用教程

    原文:[C#]WindowsAPICodePack-Shell使用教程 1.首先在项目中添加WindowsAPICodePack的Nuget包.   点击安装即可. 2.获取<我的电脑>的 ...

  8. 一些常用的UI框架

    MUI 挺好用的ui库,结合Hbuild更好用哦 WEUI 腾讯出品,和微信样式完美匹配 SUI Element UI 饿了么出品的桌面端ui框架 Mint-UI 饿了么针对移动端的ui框架 VUX ...

  9. LINQ查询表达式---------let子句

    LINQ查询表达式---------let子句 let子句创建一个范围变量来存储结果,变量被创建后,不能修改或把其他表达式的结果重新赋值给它.此范围变量可以再后续的LINQ子句中使用. class P ...

  10. C#有哪几种定时器

    1.定义在System.Windows.Forms里2.定义在System.Threading.Timer类里3.定义在System.Timers.Timer类里 System.Windows.For ...