結果

問題 No.281 門松と魔法(1)
ユーザー xuzijian629xuzijian629
提出日時 2018-10-31 07:38:08
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 1,000 ms
コード長 2,709 bytes
コンパイル時間 2,027 ms
コンパイル使用メモリ 200,944 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-30 01:02:13
合計ジャッジ時間 3,725 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using i64 = int64_t;
using vi = vector<i64>;
using vvi = vector<vi>;

int solve(int d, int a, int b, int c) {
    if (a == c) {
        assert(a == 0 && c == 0);
        return -1;
    }

    if (a < b && b > c) {
        return 0;
    }

    if (a > b && b < c) {
        return 0;
    }

    if (d == 0 || b == 0) {
        return -1;
    }

    int ans0 = -1, ans = -1;
    int k = min(a, c);
    // lower b
    if (k != 0) {
        int diff = b - (k - 1);
        ans0 = (diff + d - 1) / d;
    }

    // lower a, c
    k = max(a, c);
    if (k != 0) {
        int diff = k - (b - 1);
        int t = (diff + d - 1) / d;
        // lower c
        if (a < c) {
            int cc = max(0, c - d * t);
            int aa = a;
            if (a == b) {
                if (a > 0) {
                    aa = max(0, a - d);
                    if (aa != cc) {
                        ans = t + 1;
                    } else if (aa > 0 || cc > 0) {
                        ans = t + 2;
                    }
                }
            } else {
                if (a == cc) {
                    if (a > 0 || cc > 0) {
                        ans = t + 1;
                    }
                } else {
                    ans = t;
                }
            }
        } else {
            int aa = max(0, a - d * t);
            int cc = c;
            if (c == b) {
                if (c > 0) {
                    cc = max(0, c - d);
                    if (aa != cc) {
                        ans = t + 1;
                    } else if (aa > 0 || cc > 0) {
                        ans = t + 2;
                    }
                }
            } else {
                if (aa == c) {
                    if (aa > 0 || c > 0) {
                        ans = t + 1;
                    }
                } else {
                    ans = t;
                }
            }
        } 
    }
    if (ans0 == -1) {
        return ans;
    } else if (ans == -1) {
        return ans0;
    } else {
        return min(ans0, ans);
    }
}

int main() {
    int d;
    int a, b, c;
    cin >> d >> a >> b >> c;
    if (a != c) {
        cout << solve(d, a, b, c) << endl;
    } else {
        if (d == 0) {
            cout << -1 << endl;
            return 0;
        }
        int ret1 = 1 + solve(d, max(0, a - d), b, c);
        int ret2 = 1 + solve(d, a, b, max(0, c - d));
        if (ret1 == 0 && ret2 == 0) {
            cout << -1 << endl;
        } else if (ret1 == 0) {
            cout << ret2 << endl;
        } else if (ret2 == 0) {
            cout << ret1 << endl;
        } else {
            cout << min(ret1, ret2) << endl;
        }
    }
}
0