結果

問題 No.281 門松と魔法(1)
ユーザー TatamoTatamo
提出日時 2016-06-15 07:59:30
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 3,211 bytes
コンパイル時間 491 ms
コンパイル使用メモリ 61,552 KB
実行使用メモリ 4,512 KB
最終ジャッジ日時 2023-08-06 17:13:14
合計ジャッジ時間 2,368 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include<iostream>
#include<algorithm>

#define ANS(n) {cout << n << endl; return 0;}

using namespace std;

typedef long long ll;

int* cloneTree(int* tr){
	int *clone = new int[3];
	for(int i=0; i<3; i++){
		clone[i] = tr[i];
	}
	return clone;
}

bool check(int* tr){
	if(tr[0] == tr[1] || tr[1] == tr[2] || tr[2] == tr[0]) return false;
	if(tr[0] < tr[1] && tr[1] > tr[2]) return true;
	if(tr[0] > tr[1] && tr[1] < tr[2]) return true;
	return false;
}

// UNDONE
int main(){
	int d;
	cin >> d;

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

	// 最初から条件を満たしている
	if(check(h)) ANS(0)
	else if(d==0) ANS(-1) // むり

	bool flg_skip = false;

	// all same
	if(h[0] == h[1] && h[1] == h[2]){
		if(h[0] == 0) ANS(-1) // むり
		else if(h[0] - d > 0) ANS(3)
		else ANS(-1)
	}

	// 左右どちらかが飛び出していて残り2つは同じ
	else if( (h[0] == h[1] && h[0] < h[2]) ||
		(h[1] == h[2] && h[0] > h[1]) ){
		if(h[1] == 0) ANS(-1)
		else ANS(1)
	}

	// 左右どちらかだけ低い
	else if( (h[0] == h[1] && h[0] > h[2]) ||
		(h[1] == h[2] && h[0] < h[1]) ){
		if(min(h[0], h[2]) == 0){
			if(h[1]-d >0) ANS(1)
			else ANS(-1)
		}
		else {
			if(h[1]-d != min(h[0], h[2])) ANS(1) // 低くない方の左右端を削る
			else ANS(2)
		}
	}
	
	// 真ん中だけが飛び出している
	else if(h[0] == h[2]){
		// 真ん中が大きい
		if(h[0] < h[1]){
			if(h[0] == 0) ANS(-1)
			else ANS(1)
		}
		// 真ん中が小さい
		else{
			// 真ん中が0
			if(h[1] == 0){
				if(h[0]-d <= 0) ANS(-1)
				else ANS(1)
			}
			else{
				if(h[0]-d > h[1]) ANS(1)
				else { // 階段状
					if(h[0]-d <= 0) ANS(-1)
					else if(h[0]-d == h[1]) ANS(2)
					/*else{
						flg_skip = true;
						h[0] -= d;
						goto STAIR;
					}*/
					else ANS(3)
				}
			}
		}
	}
	// 階段状
	else if((h[0] > h[1] && h[1] > h[2]) ||
			(h[0] < h[1] && h[1] < h[2]) ){
STAIR:
		int offset = flg_skip?1:0;
		int result=-1;
		int cnt=0;
		int c_h = h[1];
		int* clone = cloneTree(h);

		int lower = h[0]<h[2]?0:2;
		int l_h = h[lower];
		int higher = h[0]>h[2]?0:2;
		int h_h = h[higher];

		if(l_h == 0){
			int tmp;
			tmp = (h_h - c_h) /d; // 高いところ削る
			if(h_h - d*tmp == c_h){
				if(h_h-d*(tmp+1) > 0) ANS(tmp+1+offset)
				else ANS(-1)
			}
			else if(h_h - d*tmp <= 0) ANS(-1)
			else ANS(tmp)
		}
		int tmp;
		tmp = (c_h - l_h) / d + 1; // 真ん中削る
		int tmp2;
		tmp2 = (h_h - c_h) / d + 1; // 高いところ削る
		if(h_h - d*tmp2 == c_h){ // 削ったら真ん中と同じ高さになった
			if(h_h - d*(tmp2+1) != l_h) tmp2 = tmp2+1; // もう1回削ったらいけた
			else{ // もう1回削ったら低い方と同じ高さになった
				tmp2 = tmp2+2; // さらに1回削ればいけるんじゃね
			}
		}
		//cerr << "tmp: " << tmp << ", tmp2: " << tmp2 << endl;
		result = tmp;
		if(tmp2 != -1) result = min(result, tmp2);
		ANS(result+offset)


		cnt = 0;
		clone = cloneTree(h);
		while(h_h>0) {// 高い方削ってみる
			cnt++;
			h_h -= d;
			if(h_h < 0) h_h = 0;
			clone[higher] = h_h;
			if(check(clone)){
				tmp=min(cnt,tmp);
				break;
			}
		}
		ANS(tmp)
	}

	// 多分ここには来ない
	ANS(-1);

}
0