- Ropewalkers(タイトル..水)

polycarpは彼の週末にリラックスすることを決定し、有名なropewalkersのパフォーマンスに訪問した:Agafon、宿屋の主人とコンラート。

ロープは、両方向のストレートと無限です。パフォーマンスの開始時に、Agafon、宿屋の主人とコンラートは位置に配置されているA

B及びCのそれぞれ。性能の終わりには、ropewalkersの各対の間の距離であった、少なくともD

Ropewalkersはロープの上を歩くことができます。1秒で、一つだけropewalkerは彼の位置を変更することができます。すべてのropewalkerはにより正確に自分の位置を変更することができます1

(すなわちシフト1

ロープ上の左または右方向へ)。Agafon、宿屋の主人とコンラートは(同時に動かすことができないだけそのうちの一つは、各瞬間に移動することができます)。Ropewalkersは同時に同じ位置にすることができ、「お互いを過ぎて歩く」ことができます。

あなたは、パフォーマンスの(秒)最小期間を見つける必要があります。ropewalkersの各対の間の距離が大きいかまたは等しいとすることができるように、換言すれば、必要な秒数の最小値を見つけるD

 

Ropewalkersが原因ロープに、負の座標に歩くことができ、両側に無限です。

入力

入力の唯一のラインは4つの整数が含まれ

B、BのC-C、CとDのD1 B C D 10 9

)。任意の2つ(または3つすべて)ropewalkersは、パフォーマンスの初めに同じ位置にあることも可能です。

出力

出力1つの整数 - 最小期間(秒)の性能。

入力
5 2 6 3
出力
2
入力
3 1 5 6
出力
8
入力
8 3 3 2
出力
2
入力
2 3 10 4
出力
3

注意

最初の例では最初の2秒でコンラッドは、位置(右に2つの位置に対して移動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;
}

 

おすすめ

転載: www.cnblogs.com/nonames/p/11221461.html