結果

問題 No.281 門松と魔法(1)
ユーザー h_noson
提出日時 2016-07-29 20:59:03
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 2 ms / 1,000 ms
コード長 1,571 bytes
コンパイル時間 467 ms
コンパイル使用メモリ 60,064 KB
実行使用メモリ 6,820 KB
最終ジャッジ日時 2024-11-06 20:47:16
合計ジャッジ時間 1,889 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 57
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

#define RREP(i,s,e) for (i = s; i >= e; i--)
#define rrep(i,n) RREP(i,n,0)
#define REP(i,s,e) for (i = s; i < e; i++)
#define rep(i,n) REP(i,0,n)
#define INF 2e9

typedef long long ll;

bool is_kadomatsu(int a, int b, int c) {
    return a != c && (b < min(a,c) || b > max(a,c));
}

int count1(int d, int h1, int h2, int h3) {
    int ret = 0;
    if (h1 >= h2) {
        int x = (h1 - h2 + d) / d;
        ret += x;
        h1 -= x * d;
        if (h1 < 0) return INF;
    }
    if (h3 >= h2) {
        int x = (h3 - h2 + d) / d;
        ret += x;
        h3 -= x * d;
        if (h3 < 0) return INF;
    }
    if (h1 == h3) {
        h1 -= d;
        ret++;
    }
    h1 = max(0,h1);
    h3 = max(0,h3);
    if (is_kadomatsu(h1,h2,h3)) return ret;
    else return INF;
}

int count2(int d, int h1, int h2, int h3) {
    int ret = 0;
    if (h1 > h3) swap(h1,h3);
    if (h1 == h3) {
        h1 -= d;
        ret++;
    }
    h1 = max(0,h1);
    if (h1 <= h2) {
        int x = (h2 - h1 + d) / d;
        ret += x;
        h2 -= x * d;
    }
    h2 = max(0,h2);
    if (is_kadomatsu(h1,h2,h3)) return ret;
    else return INF;
}

int main() {
    int d, h1, h2, h3;
    cin >> d >> h1 >> h2 >> h3;

    if (d == 0) {
        if (is_kadomatsu(h1,h2,h3)) cout << 0 << endl;
        else cout << -1 << endl;
        return 0;
    }
    int ans = min(count1(d,h1,h2,h3),count2(d,h1,h2,h3));
    if (ans == INF)
        cout << -1 << endl;
    else
        cout << ans << endl;
    return 0;
}
0