結果

問題 No.281 門松と魔法(1)
コンテスト
ユーザー tottoripaper
提出日時 2015-09-28 21:50:25
言語 C++11
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=gnu++11 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 2 ms / 1,000 ms
コード長 1,646 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 352 ms
コンパイル使用メモリ 54,912 KB
実行使用メモリ 6,400 KB
最終ジャッジ日時 2026-05-07 02:28:59
合計ジャッジ時間 2,604 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 57
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <cstdio>
#include <algorithm>
#include <limits>

int d;
int H[3];

const int INF = std::numeric_limits<int>::max();

int main(){
    scanf("%d", &d);
    for(int i=0;i<3;i++){scanf("%d", H+i);}

    if(d == 0){
        puts((H[0] == H[1] || H[1] == H[2] || H[2] == H[0] || (*std::max_element(H, H+3) != H[1] && *std::min_element(H, H+3) != H[1]))
            ? "-1"
            : "0");
        return 0;
    }
    
    int res = INF;
    
    // 中央の木を最大にしたい
    {
        int s = std::max((H[0] - H[1] + d - 1) / d, 0),
            t = std::max((H[2] - H[1] + d - 1) / d, 0);
        int a = std::max(H[0] - s * d, 0),
            b = std::max(H[2] - t * d, 0);

        if(a == H[1]){a = std::max(a - d, 0); s += 1;}
        if(b == H[1]){b = std::max(b - d, 0); t += 1;}
        
        if(a != b){
            res = s + t;
        }else if(!(a == 0 && b == 0)){
            res = s + t + 1;
        }
    }

    // printf("1st: %d\n", res);
    
    // 中央の木を最小にしたい
    {
        int count = 0;
        
        if(H[0] == H[2]){
            H[0] = std::max(H[0] - d, 0);
            count += 1;
        }

        int t = std::max({ (H[1] - H[0] + d - 1) / d
                         , (H[1] - H[2] + d - 1) / d
                         , 0
                         } );
        int x = std::max(H[1] - t * d, 0);

        if(x == H[0] || x == H[2]){
            x = std::max(x - d, 0);
            count += 1;
        }
        
        if(!((H[0] == 0 || H[2] == 0) && x == 0)){
            res = std::min(res, t + count);
        }
    }

    printf("%d\n", (res == INF) ? -1 : res);
}
0