Skip to content

Instantly share code, notes, and snippets.

@Wendly
Last active May 12, 2017 07:00
Show Gist options
  • Save Wendly/0ad2e5e8904f363a11bcdf145f85c930 to your computer and use it in GitHub Desktop.
Save Wendly/0ad2e5e8904f363a11bcdf145f85c930 to your computer and use it in GitHub Desktop.
while True:
try:
n, s, m, t = map(int, raw_input().split(' '))
H = map(int, raw_input().split(' '))
t -= 1
s -= 1
ht = 0
def calMount(H, left, right, t):
dh = 0
if H[left - 1] <= H[right + 1]:
dh = H[left - 1] - H[right]
H[right] = H[left - 1]
else:
dh = H[right + 1] - H[left]
H[left] = H[right + 1]
mount = (right - left + 1) * dh
dht = 0
if t >= left and t <= right:
dht = dh
return mount, dht
rights = []
left = s
mountLeft = 0
def runLeft(H, t, left, rights):
while left > 0:
if H[left - 1] < H[left]:
rights.append(left)
elif H[left - 1] > H[left]:
if len(rights) == 0:
return 0, left, 0
right = rights[-1]
H[right - 1] = H[left]
mount, dht = calMount(H, left, right - 1, t)
if H[left - 1] >= H[right]:
rights.pop()
if H[left - 1] <= H[right]:
left -= 1
return mount, left, float(dht)
left -= 1
return 2000000000, 0, 0
lefts = []
right = s
mountRight = 0
def runRight(H, t, right, lefts):
while right < n - 1:
if H[right + 1] < H[right]:
lefts.append(right)
elif H[right + 1] > H[right]:
if len(lefts) == 0:
return 0, right, 0
left= lefts[-1]
H[left + 1] = H[right]
mount, dht = calMount(H, left + 1, right, t)
if H[right + 1] >= H[left]:
lefts.pop()
if H[right + 1] <= H[left]:
right += 1
return mount, right, float(dht)
right += 1
return 2000000000, n - 1, 0
def runCenter(H, t, left, right):
mount, dht = calMount(H, left, right, t)
if H[left - 1] <= H[right + 1]:
left -= 1
if H[left - 1] >= H[right + 1]:
right += 1
return mount, left, right, float(dht)
def updateMount(mount, m, dMount, ht, dht):
if mount + dMount < m:
mount += dMount
ht += dht
else:
ht += dht * (m - mount) / dMount
mount = m
return mount, ht
mount = 0
while mount < m:
if mountLeft == 0:
mountLeft, left, dhtLeft = runLeft(H, t, left, rights)
if mountRight == 0:
mountRight, right, dhtRight = runRight(H, t, right, lefts)
if mountLeft == 0 and mountRight == 0:
dMount, left, right, dht = runCenter(H, t, left, right)
mount, ht = updateMount(mount, m, dMount, ht, dht)
elif mountLeft == 0:
mount, ht = updateMount(mount, m, mountRight, ht, dhtRight)
mountRight = 0
elif mountRight == 0:
mount, ht = updateMount(mount, m, mountLeft, ht, dhtLeft)
mountLeft = 0
else:
if mountLeft <= mountRight:
mount, ht = updateMount(mount, m, mountLeft * 2, ht, dhtLeft + dhtRight * mountLeft / mountRight)
dhtRight = dhtRight * (mountRight - mountLeft) / mountRight
mountRight -= mountLeft
mountLeft = 0
elif mountLeft > mountRight:
mount, ht = updateMount(mount, m, mountRight * 2, ht, dhtRight + dhtLeft * mountRight / mountLeft)
dhtLeft = dhtLeft * (mountLeft - mountRight) / mountLeft
mountLeft -= mountRight
mountRight = 0
print int(ht + 0.000000001)
except EOFError:
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment