結果

問題 No.281 門松と魔法(1)
ユーザー koyumeishikoyumeishi
提出日時 2015-09-18 23:09:52
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 1,000 ms
コード長 1,887 bytes
コンパイル時間 615 ms
コンパイル使用メモリ 76,056 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-06 17:28:01
合計ジャッジ時間 2,526 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <vector>
#include <cstdio>
#include <sstream>
#include <map>
#include <string>
#include <algorithm>
#include <queue>
#include <cmath>
#include <set>
#include <cassert>
using namespace std;

bool check(vector<long long>& h){
	if(h[0] == h[1] || h[1] == h[2] || h[2] == h[0]) return false;
	if(h[1] > h[0] && h[1] > h[2]) return true;
	if(h[1] < h[0] && h[1] < h[2]) return true;
	return false;
}

int main(){
	long long d;
	cin >> d;

	vector<long long> h(3);
	for(int i=0; i<3; i++){
		cin >> h[i];
	}

	if(check(h)){
		cout << 0 << endl;
		return 0;
	}

	//assert(d==0);


	long long ans = -1;

	if(d>0){	// h[1] > h[0],h[2]

		vector<long long> h_ = h;
		long long tmp = 0;

		long long lb = -1;
		long long ub = 1e10;
		while(ub-lb>1){
			long long mid = (ub+lb)/2;
			long long len = max(0LL, h_[0] - d*mid);
			if(len >= h_[1]){
				lb = mid;
			}else{
				ub = mid;
			}
		}

		h_[0] = max(0LL, h_[0] - d*ub);
		tmp += ub;

		lb = -1;
		ub = 1e10;
		while(ub-lb>1){
			long long mid = (ub+lb)/2;
			long long len = max(0LL, h_[2] - d*mid);
			if(len >= h_[1]){
				lb = mid;
			}else{
				ub = mid;
			}
		}

		h_[2] = max(0LL, h_[2] - d*ub);
		tmp += ub;

		if(h_[0] == h_[2]){
			h_[0] = max(0LL, h_[0] - d);
			tmp++;
		}

		if(check(h_)){
			ans = tmp;
		}
	}

	if(d>0){	// h[1] < h[0],h[2]

		vector<long long> h_ = h;
		long long tmp = 0;

		long long lb = -1;
		long long ub = 1e10;

		if(h_[0] == h_[2]){
			h_[0] = max(0LL, h_[0] - d);
			tmp++;
		}

		while(ub-lb>1){
			long long mid = (ub+lb)/2;
			long long len = max(0LL, h_[1] - d*mid);
			if(len >= h_[0] || len >= h_[2]){
				lb = mid;
			}else{
				ub = mid;
			}
		}

		h_[1] = max(0LL, h_[1] - d*ub);
		tmp += ub;

		if(check(h_)){
			if(ans == -1){
				ans = tmp;
			}else{
				ans = min(ans, tmp);
			}
		}
	}

	if(ans > 3*1e9){
		ans = -1;
	}

	cout << ans << endl;

	return 0;
}
0