結果

問題 No.281 門松と魔法(1)
ユーザー masamasa
提出日時 2016-07-19 02:42:11
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 1,000 ms
コード長 1,426 bytes
コンパイル時間 678 ms
コンパイル使用メモリ 70,988 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-24 12:42:32
合計ジャッジ時間 1,752 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 1 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 1 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 1 ms
5,376 KB
testcase_12 AC 1 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 2 ms
5,376 KB
testcase_19 AC 2 ms
5,376 KB
testcase_20 AC 1 ms
5,376 KB
testcase_21 AC 2 ms
5,376 KB
testcase_22 AC 1 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 1 ms
5,376 KB
testcase_28 AC 2 ms
5,376 KB
testcase_29 AC 1 ms
5,376 KB
testcase_30 AC 2 ms
5,376 KB
testcase_31 AC 2 ms
5,376 KB
testcase_32 AC 2 ms
5,376 KB
testcase_33 AC 1 ms
5,376 KB
testcase_34 AC 1 ms
5,376 KB
testcase_35 AC 2 ms
5,376 KB
testcase_36 AC 2 ms
5,376 KB
testcase_37 AC 1 ms
5,376 KB
testcase_38 AC 2 ms
5,376 KB
testcase_39 AC 1 ms
5,376 KB
testcase_40 AC 1 ms
5,376 KB
testcase_41 AC 1 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 1 ms
5,376 KB
testcase_47 AC 1 ms
5,376 KB
testcase_48 AC 1 ms
5,376 KB
testcase_49 AC 2 ms
5,376 KB
testcase_50 AC 2 ms
5,376 KB
testcase_51 AC 1 ms
5,376 KB
testcase_52 AC 2 ms
5,376 KB
testcase_53 AC 1 ms
5,376 KB
testcase_54 AC 1 ms
5,376 KB
testcase_55 AC 2 ms
5,376 KB
testcase_56 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <utility>
#include <string>

using namespace std;

int d, h1, h2, h3;

int calc_h2max() {
	int ans = 0;
	int a = h1;
	int b = h2;
	int c = h3;

	if (b < 2) {
		return -1;
	}

	int n;

	if (b <= a) {
		n = (a - b) / d + 1;
		a -= n * d;
		a = max(a, 0);
		ans += n;
	}

	if (b <= c) {
		n = (c - b) / d + 1;
		c -= n * d;
		c = max(c, 0);
		ans += n;
	}

	if (a != c) {
		return ans;
	}

	if (a == 0) {
		return -1;
	}

	return ans + 1;

}

int calc_h2min() {

	int ans = 0;
	int a = h1;
	int b = h2;
	int c = h3;

	int n;

	int min_ac = min(a, c);

	if (min_ac < 1) {
		return -1;
	}

	if (b >= min_ac) {
		n = (b - min_ac) / d + 1;
		b -= n * d;
		b = max(0, b);
		ans += n;
	}

	if (a != c) {
		return ans;
	}

	a = max(0, a - d);
	ans++;
	if (b < a) {
		return ans;
	}

	b = max(0, b - d);
	ans++;
	if (b != a) {
		return ans;
	}

	return -1;
}


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

	int ans;
	if (d == 0) {
		if (h1 == h2 || h2 == h3 || h3 == h1) {
			ans = -1;
		} else {
			int mini = min({h1, h2, h3});
			int maxi = max({h1, h2, h3});
			ans = (mini == h2 || maxi == h2) ? 0 : -1;
		}
		cout << ans << endl;
		return 0;
	}

	int ans1 = calc_h2max();
	int ans2 = calc_h2min();


	if (ans1 == -1) {
		ans = ans2;
	} else if (ans2 == -1) {
		ans = ans1;
	} else {
		ans = min(ans1, ans2);
	}

	cout << ans << endl;
	return 0;
}
0