結果

問題 No.281 門松と魔法(1)
ユーザー bal4ubal4u
提出日時 2019-06-03 21:10:51
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 1 ms / 1,000 ms
コード長 1,563 bytes
コンパイル時間 223 ms
コンパイル使用メモリ 31,216 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-17 23:29:23
合計ジャッジ時間 1,671 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

// yukicoder: 281 門松と魔法(1)
// 2019.6.3 bal4u

#include <stdio.h>

#define INF 0x55555555

int check(int *h) {
	if (h[0] == h[1] || h[1] == h[2] || h[0] == h[2]) return 0;
	if (h[0] < h[2]) {
		return (h[1] < h[0] || h[1] > h[2]);
	} else {
		return (h[1] > h[0] || h[1] < h[2]);
	}
}

int d, ans;
void rec(int *h, int id, int c) {
	int i, t;
	if (id == 3) {
		if (check(h) && c < ans) ans = c;
		return;
	}
	t = h[id];
	for (i = 0; i <= 2; i++) {
		if (h[id] < 0) h[id] = 0;
		rec(h, id+1, c+i);
		if (h[id] == 0) break;
		h[id] -= d;
	}
	h[id] = t;
}

void cutr(int h1, int h2, int h3) {
	int g[3];
	g[0] = h1, g[1] = h2;
	int t = (h3-h2-1)/d+1;
	h3 -= t*d;
	if (h3 < 0) h3 = 0;
	g[2] = h3;
	if (check(g)) {
		if (t < ans) ans = t;
		return;
	}
	h3 -= d;
	if (h3 < 0) h3 = 0;
	g[2] = h3, t++;
	if (check(g)) {
		if (t < ans) ans = t;
	}
}

void cutc(int h1, int h2, int h3) {
	int g[3];
	g[0] = h1, g[2] = h3;
	int t = (h2-h1-1)/d+1;
	h2 -= t*d;
	if (h2 < 0) h2 = 0;
	g[1] = h2;
	if (check(g)) {
		if (t < ans) ans = t;
		return;
	}
	h2 -= d;
	if (h2 < 0) h3 = 0;
	g[2] = h2, t++;
	if (check(g)) {
		if (t < ans) ans = t;
	}
}

int main()
{
	int h[3];

	scanf("%d%d%d%d", &d, h, h+1, h+2);
	if (d == 0) {
		puts(check(h)? "0": "-1");
		return 0;
	}
	ans = INF;
	rec(h, 0, 0);
	if (ans == INF) {
		if (h[0] < h[1] && h[1] < h[2]) {
			cutr(h[0], h[1], h[2]);
			cutc(h[0], h[1], h[2]);
		}
		else if (h[0] > h[1] && h[1] > h[2]) {
			cutr(h[2], h[1], h[0]);
			cutc(h[2], h[1], h[0]);
		}
	}
	if (ans == INF) ans = -1;
	printf("%d\n", ans);
	return 0;
}
0