結果

問題 No.281 門松と魔法(1)
ユーザー parukiparuki
提出日時 2016-03-19 22:34:19
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 1,000 ms
コード長 2,443 bytes
コンパイル時間 773 ms
コンパイル使用メモリ 87,224 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-24 12:40:52
合計ジャッジ時間 2,041 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 1 ms
5,376 KB
testcase_08 AC 1 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 1 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 1 ms
5,376 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 1 ms
5,376 KB
testcase_15 AC 1 ms
5,376 KB
testcase_16 AC 1 ms
5,376 KB
testcase_17 AC 1 ms
5,376 KB
testcase_18 AC 2 ms
5,376 KB
testcase_19 AC 1 ms
5,376 KB
testcase_20 AC 1 ms
5,376 KB
testcase_21 AC 1 ms
5,376 KB
testcase_22 AC 2 ms
5,376 KB
testcase_23 AC 1 ms
5,376 KB
testcase_24 AC 1 ms
5,376 KB
testcase_25 AC 2 ms
5,376 KB
testcase_26 AC 1 ms
5,376 KB
testcase_27 AC 1 ms
5,376 KB
testcase_28 AC 1 ms
5,376 KB
testcase_29 AC 1 ms
5,376 KB
testcase_30 AC 1 ms
5,376 KB
testcase_31 AC 2 ms
5,376 KB
testcase_32 AC 1 ms
5,376 KB
testcase_33 AC 1 ms
5,376 KB
testcase_34 AC 1 ms
5,376 KB
testcase_35 AC 1 ms
5,376 KB
testcase_36 AC 1 ms
5,376 KB
testcase_37 AC 2 ms
5,376 KB
testcase_38 AC 1 ms
5,376 KB
testcase_39 AC 1 ms
5,376 KB
testcase_40 AC 2 ms
5,376 KB
testcase_41 AC 1 ms
5,376 KB
testcase_42 AC 2 ms
5,376 KB
testcase_43 AC 1 ms
5,376 KB
testcase_44 AC 2 ms
5,376 KB
testcase_45 AC 1 ms
5,376 KB
testcase_46 AC 2 ms
5,376 KB
testcase_47 AC 1 ms
5,376 KB
testcase_48 AC 2 ms
5,376 KB
testcase_49 AC 1 ms
5,376 KB
testcase_50 AC 1 ms
5,376 KB
testcase_51 AC 1 ms
5,376 KB
testcase_52 AC 2 ms
5,376 KB
testcase_53 AC 1 ms
5,376 KB
testcase_54 AC 2 ms
5,376 KB
testcase_55 AC 2 ms
5,376 KB
testcase_56 AC 1 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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();
    }
}
0