結果

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

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <vector>

using lint = long long;

bool judge(const std::vector<lint>& hs) {
    return ((hs[0] < hs[1] && hs[1] > hs[2]) ||
            (hs[0] > hs[1] && hs[1] < hs[2])) &&
           hs[0] != hs[2];
}

constexpr lint INF = 1LL << 60;

void solve() {
    lint d;
    std::cin >> d;
    std::vector<lint> hs(3);
    for (auto& h : hs) std::cin >> h;

    if (d == 0) {
        std::cout << (judge(hs) ? 0 : -1) << "\n";
        return;
    }

    lint ans = INF;
    {
        auto nhs = hs;
        lint cost = 0;
        for (int i = 0; i < 3; i += 2) {
            lint x = (nhs[i] < nhs[1] ? 0 : (nhs[i] - nhs[1]) / d + 1);
            nhs[i] = std::max(0LL, nhs[i] - x * d);
            cost += x;
        }

        if (nhs[0] == nhs[2]) {
            nhs[0] = std::max(0LL, nhs[0] - d);
            ++cost;
        }

        if (judge(nhs)) ans = std::min(ans, cost);
    }

    {
        auto nhs = hs;
        lint cost = 0;

        if (nhs[0] == nhs[2]) {
            nhs[0] = std::max(0LL, nhs[0] - d);
            ++cost;
        }

        for (int i = 0; i < 3; i += 2) {
            lint x = (nhs[1] < nhs[i] ? 0 : (nhs[1] - nhs[i]) / d + 1);
            nhs[1] = std::max(0LL, nhs[1] - x * d);
            cost += x;
        }

        if (judge(nhs)) ans = std::min(ans, cost);
    }

    std::cout << (ans == INF ? -1 : ans) << "\n";
}

int main() {
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);

    solve();

    return 0;
}
0