結果

問題 No.281 門松と魔法(1)
ユーザー startcpp
提出日時 2015-09-19 00:06:36
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 2 ms / 1,000 ms
コード長 1,848 bytes
コンパイル時間 449 ms
コンパイル使用メモリ 56,180 KB
実行使用メモリ 6,820 KB
最終ジャッジ日時 2024-11-06 20:28:35
合計ジャッジ時間 1,902 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 57
権限があれば一括ダウンロードができます

ソースコード

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