結果

問題 No.281 門松と魔法(1)
コンテスト
ユーザー pekempey
提出日時 2015-11-27 00:27:11
言語 PyPy2
(7.3.15)
結果
AC  
実行時間 94 ms / 1,000 ms
コード長 857 bytes
コンパイル時間 1,689 ms
コンパイル使用メモリ 76,712 KB
実行使用メモリ 75,776 KB
最終ジャッジ日時 2024-11-06 20:43:36
合計ジャッジ時間 8,160 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 57
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

INF = 10 ** 18

d, h1, h2, h3 = [input() for _ in xrange(4)]

def is_kadomatsu(a, b, c):
	return a != c and (a < b and b > c or a > b and b < c)

if is_kadomatsu(h1, h2, h3):
	print 0
	sys.exit()

if d == 0:
	print -1
	sys.exit()

f = lambda x: max(0, x - d)

ans = 0
if h1 == h3:
	h1 = f(h1)
	ans += 1

if h1 == h3:
	print -1
	sys.exit()

# h1 < h3 and h3 > h2
ans1 = ans
t1, t2, t3 = h1, h2, h3

k1 = max(0, (t1 - t2 + d) / d)
k3 = max(0, (t3 - t2 + d) / d)

t1 = max(0, t1 - k1 * d)
t3 = max(0, t3 - k3 * d)
ans1 = ans + k1 + k3
if t1 == t3:
	t1 = f(t1)
	ans1 += 1

if not is_kadomatsu(t1, t2, t3):
	ans1 = INF

# h1 > h3 and h3 < h2
t1, t2, t3 = h1, h2, h3
k2 = max(0, (t2 - min(t1, t3) + d) / d)
ans2 = ans + k2

t2 = max(0, t2 - k2 * d)
if not is_kadomatsu(t1, t2, t3):
	ans2 = INF

ans = min(ans1, ans2)
if ans == INF: ans = -1
print ans
0