結果

問題 No.281 門松と魔法(1)
ユーザー startcppstartcpp
提出日時 2015-09-19 00:06:36
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 1,000 ms
コード長 1,848 bytes
コンパイル時間 372 ms
コンパイル使用メモリ 57,168 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-24 12:35:08
合計ジャッジ時間 1,599 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 1 ms
5,376 KB
testcase_08 AC 1 ms
5,376 KB
testcase_09 AC 1 ms
5,376 KB
testcase_10 AC 1 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 1 ms
5,376 KB
testcase_13 AC 1 ms
5,376 KB
testcase_14 AC 1 ms
5,376 KB
testcase_15 AC 1 ms
5,376 KB
testcase_16 AC 1 ms
5,376 KB
testcase_17 AC 1 ms
5,376 KB
testcase_18 AC 1 ms
5,376 KB
testcase_19 AC 1 ms
5,376 KB
testcase_20 AC 1 ms
5,376 KB
testcase_21 AC 1 ms
5,376 KB
testcase_22 AC 1 ms
5,376 KB
testcase_23 AC 1 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 1 ms
5,376 KB
testcase_29 AC 2 ms
5,376 KB
testcase_30 AC 1 ms
5,376 KB
testcase_31 AC 1 ms
5,376 KB
testcase_32 AC 1 ms
5,376 KB
testcase_33 AC 1 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 1 ms
5,376 KB
testcase_38 AC 1 ms
5,376 KB
testcase_39 AC 2 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 1 ms
5,376 KB
testcase_44 AC 2 ms
5,376 KB
testcase_45 AC 1 ms
5,376 KB
testcase_46 AC 1 ms
5,376 KB
testcase_47 AC 1 ms
5,376 KB
testcase_48 AC 2 ms
5,376 KB
testcase_49 AC 1 ms
5,376 KB
testcase_50 AC 1 ms
5,376 KB
testcase_51 AC 1 ms
5,376 KB
testcase_52 AC 1 ms
5,376 KB
testcase_53 AC 1 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 #

//最高位はたたかない。k位にするものはk-1位より短くなるまで叩く。ただし負になったらGEND。
#include <iostream>
#include <cassert>
#define LL long long
using namespace std;

LL d;
LL h[4];

int main() {
	LL H[4];
	LL N[4];
	LL ans = 1145141919893810;
	
	cin >> d;
	cin >> h[1] >> h[2] >> h[3];
	
	//d == 0の場合は別途処理
	if( d == 0 ){
		bool ok = false;
		ok |= ( h[1] < h[3] && h[3] < h[2] );
		ok |= ( h[3] < h[1] && h[1] < h[2] );
		ok |= ( h[2] < h[1] && h[1] < h[3] );
		ok |= ( h[2] < h[3] && h[3] < h[1] );
		if( ok )
			cout << 0 << endl;
		else
			cout << -1 << endl;
		return 0;
	}
	
	//h[1] < h[3] < h[2]
	N[2] = 0;
	H[2] = h[2];
	N[3] = max((LL)0, (h[3] - H[2] + d)/d);
	H[3] = max((LL)0, h[3] - N[3] * d);
	N[1] = max((LL)0, (h[1] - H[3] + d)/d);
	H[1] = max((LL)0, h[1] - N[1] * d);
	if( H[1] < H[3] && H[3] < H[2] )
		ans = min(ans, N[1] + N[2] + N[3]);
		
	//h[3] < h[1] < h[2]
	N[2] = 0;
	H[2] = h[2];
	N[1] = max((LL)0, (h[1] - H[2] + d)/d);
	H[1] = max((LL)0, h[1] - N[1] * d);
	N[3] = max((LL)0, (h[3] - H[1] + d)/d);
	H[3] = max((LL)0, h[3] - N[3] * d);
	if( H[3] < H[1] && H[1] < H[2] )
		ans = min(ans, N[1] + N[2] + N[3]);
	
	//h[2] < h[1] < h[3]
	N[3] = 0;
	H[3] = h[3];
	N[1] = max((LL)0, (h[1] - H[3] + d)/d);
	H[1] = max((LL)0, h[1] - N[1] * d);
	N[2] = max((LL)0, (h[2] - H[1] + d)/d);
	H[2] = max((LL)0, h[2] - N[2] * d);
	if( H[2] < H[1] && H[1] < H[3] )
		ans = min(ans, N[1] + N[2] + N[3]);
		
	//h[2] < h[3] < h[1]
	N[1] = 0;
	H[1] = h[1];
	N[3] = max((LL)0, (h[3] - H[1] + d)/d);
	H[3] = max((LL)0, h[3] - N[3] * d);
	N[2] = max((LL)0, (h[2] - H[3] + d)/d);
	H[2] = max((LL)0, h[2] - N[2] * d);
	if( H[2] < H[3] && H[3] < H[1] )
		ans = min(ans, N[1] + N[2] + N[3]);
	
	if( ans == 1145141919893810 ){
		cout << -1 << endl;
	}
	else
		cout << ans << endl;
	return 0;
}
0