結果
| 問題 | 
                            No.281 門松と魔法(1)
                             | 
                    
| コンテスト | |
| ユーザー | 
                             | 
                    
| 提出日時 | 2016-03-19 22:34:19 | 
| 言語 | C++11(廃止可能性あり)  (gcc 13.3.0)  | 
                    
| 結果 | 
                             
                                AC
                                 
                             
                            
                         | 
                    
| 実行時間 | 2 ms / 1,000 ms | 
| コード長 | 2,443 bytes | 
| コンパイル時間 | 875 ms | 
| コンパイル使用メモリ | 87,332 KB | 
| 実行使用メモリ | 5,248 KB | 
| 最終ジャッジ日時 | 2024-11-06 20:44:42 | 
| 合計ジャッジ時間 | 2,348 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge3 / judge2 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 57 | 
ソースコード
#include <cstring>
#include <cmath>
#include <cstdio>
#include <cassert>
#include <climits>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <deque>
#include <utility>
#include <algorithm>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <bitset>
#include <functional>
#include <numeric>
#include <iomanip>
#include <iterator>
#include <array>
using namespace std;
#define rt return
#define FOR(i,j,k) for(int i=j; i<(int)k;++i)
#define rep(i,j) for(int i=0;i<(int)j;++i)
#define rep1(i,j) for(int i=1;i<=(int)j;++i)
#define each(x,y) for(auto &(x):(y))
#define mp make_pair
#define mt make_tuple
#define pb_ push_back
#define eb_ emplace_back
#define fi_ first
#define se_ second
#define all(x) (x).begin(),(x).end()
#define debug(x) cout<<#x<<": "<<(x)<<endl
#define smax(x,y) (x)=max((x),(y))
#define smin(x,y) (x)=min((x),(y))
#define MEM(x,y) memset((x),(y),sizeof (x))
#define sz(x) (int)(x).size()
#define p_ printf
#define s_ scanf
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
int D, H1, H2, H3;
void f(){
    if(D == 0){
        if(H1 != H2&&H2 != H3&&H3 != H1 && ((H2<H1 && H2<H3) || (H2 > H1&&H2 > H3))){
            cout << 0 << endl;
        } else{
            cout << -1 << endl;
        }
        rt;
    }
    // h2が低い
    ll h1 = H1, h2 = H2, h3 = H3, ans = LLONG_MAX;
    if(h1 != 0 && h3 != 0){
        ll tmp = 0;
        if(h1 > h3)swap(h1, h3);
        if(h1 == h3) h1 = max(0ll, h1 - D), tmp = 1;
        
        if(h1){
            if(h2 >= h1){
                tmp += (h2 - h1 + D) / D;
                h2 = max(0ll, h2 - (h2 - h1 + D) / D*D);
            }
            if(h1 != h2 && h2 != h3&&h3 != h1){
                smin(ans, tmp);
            }
        }
    }
    //h2が高い
    ll tmp = 0;
    h1 = H1, h2 = H2, h3 = H3;
    
    if(h1 >= h2){
        ll cost = (h1 - h2 + D) / D;
        h1 = max(0ll, h1 - cost*D);
        tmp += cost;
    }
    if(h3 >= h2){
        ll cost = (h3 - h2 + D) / D;
        h3 = max(0ll, h3 - cost*D);
        tmp += cost;
    }
    if(h1 > h3)swap(h1, h3);
    if(h1 == h3){
        ++tmp;
        h1 = max(0ll, h1 - D);
    }
    if(h1 != h2&&h2 != h3&&h3 != h1)smin(ans, tmp);
    if(ans != LLONG_MAX)cout << ans << endl;
    else cout << -1 << endl;
}
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    while(cin >> D >> H1 >> H2 >> H3){
        f();
    }
}