码迷,mamicode.com
首页 > 其他好文 > 详细

A - Ropewalkers (水题。。)

时间:2019-07-21 16:16:41      阅读:92      评论:0      收藏:0      [点我收藏+]

标签:arp   The   minus   des   line   ons   ble   data-   xmlns   

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

,a b,b cc,c dd (1a,b,c,d109

). 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 |52|=3, the distance between Boniface and Konrad will be |28|=6 and the distance between Agafon and Konrad will be |58|=3. Therefore, all three pairwise distances will be at least d=3, so the performance could be finished within 2 seconds.

 

思路:排序将最大与最小数往两边操作就ok。。

 

#include <iostream>
#include <iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#include <stdio.h>
#include <string.h>
#include <vector>
#include <set>
using namespace std;
int main()
{
    int aa , b , c , d ;
    while(cin >> aa >> b >> c >> d)
    {
        a[0] = aa;
        a[1] = b ;
        a[2] = c ;
        sort(a , a+3);
        if(a[2] - a[1] >= d) // 如果最大的那个数与中间的数的距离符合要求
        {
            if(a[1] - a[0] >= d)// 最小的数与中间的数的距离符合要求
                cout << 0 <<endl ;
            else
            {
                cout <<  d - a[1] + a[0] << endl ;
            }
        }
        else{
            if(a[1] - a[0] >= d)
                cout << d - a[2] + a[1] << endl ;
            else
                cout << d - a[2] + a[1] + d - a[1] + a[0] << endl ;
        }
    }


    return 0;
}

 

A - Ropewalkers (水题。。)

标签:arp   The   minus   des   line   ons   ble   data-   xmlns   

原文地址:https://www.cnblogs.com/nonames/p/11221461.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!