Codeforces Round #568 (Div. 2)A
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的更多相关文章
- Codeforces Round #568 (Div. 2)B
B. Email from Polycarp 题目链接:http://codeforces.com/contest/1185/problem/B 题目: Methodius received an e ...
- codeforces Round #568(Div.2)A B C
有点菜,只写出了三道.活不多说,上题开干. A. Ropewalkers Polycarp decided to relax on his weekend and visited to the per ...
- Codeforces Round #568 (Div. 2) D. Extra Element
链接: https://codeforces.com/contest/1185/problem/D 题意: A sequence a1,a2,-,ak is called an arithmetic ...
- 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 ...
- Codeforces Round #568 (Div. 2) B. Email from Polycarp
链接: https://codeforces.com/contest/1185/problem/B 题意: Methodius received an email from his friend Po ...
- Codeforces Round #568 (Div. 2) A.Ropewalkers
链接: https://codeforces.com/contest/1185/problem/A 题意: Polycarp decided to relax on his weekend and v ...
- Codeforces Round #568 (Div. 2) G1. Playlist for Polycarp (easy version) (状压dp)
题目:http://codeforces.com/contest/1185/problem/G1 题意:给你n给选项,每个选项有个类型和价值,让你选择一个序列,价值和为m,要求连续的不能有两个相同的类 ...
- Codeforces Round #568 Div. 2
没有找到这场div3被改成div2的理由. A:签到. #include<bits/stdc++.h> using namespace std; #define ll long long ...
- Codeforces Round #568 (Div. 2) G2. Playlist for Polycarp (hard version)
因为不会打公式,随意就先将就一下? #include<cstdio> #include<algorithm> #include<iostream> #include ...
随机推荐
- 狄利克雷过程(Dirichlet Process)
0. 引入 现观察得到两个样本 θ1,θ2,来推测它们可能来自的分布: 假设来自于连续型概率密度函数, θ1,θ2∼H(θ) 则 θ1,θ2 相等的概率为 0,p(θ1=θ2)=0 概率为 0,不代表 ...
- MIT墙上的格言(如果你把任务留到最后一分钟,那么你一定能在一分钟内完成任务)
1,永远不能忘记傅立叶变换. Never far no can forget Fuliye changer. 2,盲目的研究者就像法拉第和麦克斯韦之间的电学家一样无所适从. Blind Eye's r ...
- 系统引导文件之 boot.ini(有很多参数)
Windows NT类的操作系统,也就是Windows NT/2000/XP中,有一个特殊文件,也就是“BOOT.INI”文件,这个文件会很轻松地按照我们的需求设置好多重启动系统. “BOOT.INI ...
- Ubuntu更改 resolv.conf 重启失效
更改Ubuntu的 resolv.conf的时候,重启的时候,经常又给重置了.google大法找到方法. sudo apt-get install resolvconf 原来Ubuntu的resol ...
- 专访Jeffrey Richter:Windows 8是微软的重中之重
Jeffrey Richter 以其多本 Windows 核心技术的经典著作而闻名,同时,他深入掌握微软的 .NET 等一系列核心技术,他所创办的 Wintellect 公司与微软有密切的合作关系,他 ...
- php 二维数组相同值 相加
array(3) { [0]=> array(2) { ["sourcesid"]=> int(1) ["addusernum"]=> str ...
- 图像滤镜艺术---Oilpaint油画滤镜
原文:图像滤镜艺术---Oilpaint油画滤镜 Oilpaint油画滤镜 图像油画效果实际上是将图像边缘产生一种朦胧,雾化的效果,同时,将一定的边缘模糊化,这样图像整体上看去像素与像素之间 ...
- PySide——Python图形化界面入门教程(六)
PySide——Python图形化界面入门教程(六) ——QListView和QStandardItemModel 翻译自:http://pythoncentral.io/pyside-pyqt-tu ...
- Android零基础入门第2节:Android 系统架构和应用组件那些事
原文:Android零基础入门第2节:Android 系统架构和应用组件那些事 继上一期浅谈了Android的前世今生,这一期一起来大致回顾一下Android 系统架构和应用组件. 一.Android ...
- Android实现dialog时候弹出软键盘dialog移位问题
Window win = getWindow(); WindowManager.LayoutParams params = win.getAttributes(); win.setSoftInputM ...