結果

問題 No.281 門松と魔法(1)
ユーザー maine_honzuki
提出日時 2021-05-12 21:13:34
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 2 ms / 1,000 ms
コード長 1,255 bytes
コンパイル時間 2,060 ms
コンパイル使用メモリ 197,084 KB
最終ジャッジ日時 2025-01-21 10:19:05
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 57
権限があれば一括ダウンロードができます

ソースコード

diff #

//https://ncode.syosetu.com/n4830bu/281/
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

int main() {
    ll d;
    vector<ll> H(3);
    cin >> d;
    for (auto&& h : H) {
        cin >> h;
    }
    if ((H[0] < H[1] && H[1] > H[2] && H[0] != H[2]) || (H[0] > H[1] && H[1] < H[2] && H[0] != H[2])) {
        cout << 0 << endl;
        return 0;
    }
    if (d == 0) {
        cout << -1 << endl;
        return 0;
    }
    ll maine = 1e11;
    auto book = [&](ll& height, ll threshold) {
        if (threshold == 0)
            return (ll)1e11;
        if (height < threshold)
            return 0ll;
        ll ret = (height - threshold + d) / d;
        height -= ret * d;
        height = max(height, 0ll);
        return ret;
    };
    {
        //low-high-low;
        auto h = H;
        ll score = book(h[0], h[1]) + book(h[2], h[1]);
        if (h[0] == h[2])
            score += book(h[0], h[2]);
        maine = min(maine, score);
    }
    {
        //high-low-high
        auto h = H;
        ll score = 0;
        if (h[0] == h[2])
            score += book(h[0], h[2]);
        score += book(h[1], h[0]) + book(h[1], h[2]);
        maine = min(maine, score);
    }
    cout << (maine > 1e10 ? -1 : maine) << endl;
}
0