結果

問題 No.281 門松と魔法(1)
ユーザー ant2357ant2357
提出日時 2017-04-25 23:32:10
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 1,000 ms
コード長 2,329 bytes
コンパイル時間 2,038 ms
コンパイル使用メモリ 201,184 KB
実行使用メモリ 4,356 KB
最終ジャッジ日時 2023-10-11 12:55:54
合計ジャッジ時間 4,272 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include "bits/stdc++.h"

#define ALL(a) (a).begin(),(a).end()
#define SORT(c) sort((c).begin(),(c).end())
#define DESCSORT(c) sort(c.begin(), c.end(), greater<int>())

using namespace std;

using LL = long long int;
using LD = long double;

using pii = pair<int, int>;
using pll = pair<LL, LL>;
using pdd = pair<double, double>;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using vl = vector<LL>;
using vvl = vector<vl>;
using vvvl = vector<vvl>;
using vd = vector<double>;
using vvd = vector<vd>;
using vs = vector<string>;
using vb = vector<bool>;
using vvb = vector<vb>;

const int INF = (1 << 30) - 1;
const LL INF64 = ((LL)1 << 62) - 1;
const double PI = 3.1415926535897932384626433832795;

const int dy[] = { 0, 1, 0, -1 };
const int dx[] = { 1, 0, -1, 0 };

int gcd(int x, int y) { return y ? gcd(y, x % y) : x; }
LL gcd(LL x, LL y) { return y ? gcd(y, x % y) : x; }

bool kadomatuCheck(vi h) {
	if (h[0] == h[1]) {
		return false;
	} else if (h[0] == h[2]) {
		return false;
	} else if (h[1] == h[2]) {
		return false;
	}

	return (h[0] < h[1] && h[1] > h[2]) || (h[0] > h[1] && h[1] < h[2]);
}

int magic(int v, int d, int times = 1) {
	v -= d * times;
	return  (v < 0 ? 0 : v);
}

int solveMax(vi h, int d) {
	if (h[1] == 0) {
		return INF;
	}

	int res = 0;

	if (h[0] >= h[1]) {
		res += (h[0] - h[1]) / d + 1;
		h[0] = magic(h[0], d, (h[0] - h[1]) / d + 1);
	}
	if (h[2] >= h[1]) {
		res += (h[2] - h[1]) / d + 1;
		h[2] = magic(h[2], d, (h[2] - h[1]) / d + 1);
	}

	if (h[0] == h[2]) {
		res++;
		h[0] = magic(h[0], d);

		if (h[0] == h[2]) {
			return INF;
		}
	}

	return (kadomatuCheck(h) ? res : INF);
}

int solveMin(vi h, int d) {
	int res = 0;

	if (h[0] == h[2]) {
		res++;
		h[0] = magic(h[0], d);
	}

	if (h[0] == 0 && h[2] == 0) {
		return INF;
	}

	if (min(h[0], h[2]) <= h[1]) {
		res += (h[1] - min(h[0], h[2])) / d + 1;
		h[1] = magic(h[1], d, (h[1] - min(h[0], h[2])) / d + 1);
	}

	return (kadomatuCheck(h) ? res : INF);
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);

	int d;
	cin >> d;

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

	if (kadomatuCheck(h)) {
		cout << 0 << endl;
	} else if (d == 0) {
		cout << -1 << endl;
	} else {
		int ans = min(solveMax(h, d), solveMin(h, d));
		cout << (ans == INF ? -1 : ans) << endl;
	}
	return 0;
}
0