AtCoder Regular Contest 072 - Dam

You are in charge of controlling a dam. The dam can store at most L liters of water. Initially, the dam is empty. Some amount of water flows into the dam every morning, and any amount of water may be discharged every night, but this amount needs to be set so that no water overflows the dam the next morning.
It is known that v_i liters of water at t_i degrees Celsius will flow into the dam on the morning of the i-th day.
You are wondering about the maximum possible temperature of water in the dam at noon of each day, under the condition that there needs to be exactly L liters of water in the dam at that time. For each i, find the maximum possible temperature of water in the dam at noon of the i-th day. Here, consider each maximization separately, that is, the amount of water discharged for the maximization of the temperature on the i-th day, may be different from the amount of water discharged for the maximization of the temperature on the j-th day (j≠i).
Also, assume that the temperature of water is not affected by anything but new water that flows into the dam. That is, when V_1 liters of water at T_1 degrees Celsius and V_2 liters of water at T_2 degrees Celsius are mixed together, they will become V_1+V_2 liters of water at \frac{T_1*V_1+T_2*V_2}{V_1+V_2} degrees Celsius, and the volume and temperature of water are not affected by any other factors.

-----Constraints-----
 - 1≤ N ≤ 5*10^5
 - 1≤ L ≤ 10^9
 - 0≤ t_i ≤ 10^9(1≤i≤N)
 - 1≤ v_i ≤ L(1≤i≤N)
 - v_1 = L
 - L, each t_i and v_i are integers.

-----Input-----
Input is given from Standard Input in the following format:
N L
t_1 v_1
t_2 v_2
:
t_N v_N

-----Output-----
Print N lines. The i-th line should contain the maximum temperature such that it is possible to store L liters of water at that temperature in the dam at noon of the i-th day.
Each of these values is accepted if the absolute or relative error is at most 10^{-6}.

-----Sample Input-----
3 10
10 10
20 5
4 3

-----Sample Output-----
10.0000000
15.0000000
13.2000000

 - On the first day, the temperature of water in the dam is always 10 degrees: the temperature of the only water that flows into the dam on the first day.
 - 10 liters of water at 15 degrees of Celsius can be stored on the second day, by discharging 5 liters of water on the night of the first day, and mix the remaining water with the water that flows into the dam on the second day.
 - 10 liters of water at 13.2 degrees of Celsius can be stored on the third day, by discharging 8 liters of water on the night of the first day, and mix the remaining water with the water that flows into the dam on the second and third days.

Source: atcoder | Difficulty: VERY_HARD


Solutions:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
from collections import deque
(N, L) = map(int, readline().split())
m = map(int, read().split())
TV = zip(m, m)
answer = []
INF = 10 ** 18
(t, v) = next(TV)
answer.append(t)
qT = deque([t])
qV = deque([v])
sum_q = t * v
for (t, v) in TV:
	qT.appendleft(t)
	qV.appendleft(v)
	sum_q += t * v
	rest = v
	while qV[-1] <= rest:
		x = qV.pop()
		rest -= x
		sum_q -= x * qT.pop()
	qV[-1] -= rest
	sum_q -= rest * qT[-1]
	while len(qT) >= 2:
		t0 = qT[0]
		t1 = qT[1]
		if t0 > t1:
			break
		qT.popleft()
		v0 = qV.popleft()
		v1 = qV[0]
		t2 = (t0 * v0 + t1 * v1) / (v0 + v1)
		qT[0] = t2
		qV[0] += v0
	answer.append(sum_q / L)
print('\n'.join(map(str, answer)))


from collections import deque
import sys

def MI():
	return map(int, sys.stdin.readline().split())

class water:

	def __init__(self, t, v):
		self.v = v
		self.tv = v * t

	def __le__(self, other):
		return self.v * other.tv - self.tv * other.v >= 0

	def __isub__(self, other):
		t = self.tv / self.v
		self.v -= other
		self.tv = t * self.v
		return self

	def __iadd__(self, other):
		self.v += other.v
		self.tv += other.tv
		return self

def main():
	(n, l) = MI()
	dam = deque()
	(t, v) = MI()
	print(t)
	dam.append(water(t, v))
	stv = t * v
	for _ in range(n - 1):
		(t, v) = MI()
		dam.appendleft(water(t, v))
		over = v
		stv += t * v
		while dam[-1].v <= over:
			w = dam.pop()
			over -= w.v
			stv -= w.tv
		stv -= dam[-1].tv
		dam[-1] -= over
		stv += dam[-1].tv
		print(stv / l)
		while len(dam) > 1 and dam[0] <= dam[1]:
			w = dam.popleft()
			dam[0] += w
main()


from collections import deque
(N, L) = list(map(int, input().split()))
T = []
V = []
for i in range(N):
	(t, v) = list(map(int, input().split()))
	T.append(t)
	V.append(v)
dq = deque()
(ct, cv) = (0.0, 0.0)
for i in range(N):
	while cv + V[i] > L:
		(ft, fv) = dq[0]
		take = min(cv + V[i] - L, fv)
		(ct, cv) = ((ct * cv - ft * take) / (cv - take), cv - take)
		if take == fv:
			dq.popleft()
		else:
			dq[0] = [ft, fv - take]
	(ct, cv) = ((ct * cv + T[i] * V[i]) / L, L)
	print('%.7f' % ct)
	while len(dq):
		(bt, bv) = dq[len(dq) - 1]
		if bt < T[i]:
			break
		(T[i], V[i]) = ((T[i] * V[i] + bt * bv) / (V[i] + bv), V[i] + bv)
		dq.pop()
	dq.append([T[i], V[i]])


from collections import deque
TT_list = []
(N, L) = map(int, input().split())
T = 0.0
vt_now = 0.0
v_now = 0
que = deque()
for i in range(N):
	(ti, v) = map(int, input().split())
	t = float(ti)
	v_now += v
	vt_now += v * t
	if v == L:
		que.append([t, v])
	else:
		while v < L and len(que) > 0:
			(t_, v_) = que[-1]
			if t_ <= t:
				que.append([t, v])
				break
			elif v + v_ >= L:
				v_ = v + v_ - L
				t = ((L - v) * t_ + v * t) / L
				v = L
				que = deque([[t, v]])
				v_now = L
				vt_now = t * L
			else:
				t = (t * v + t_ * v_) / (v + v_)
				v = v + v_
				que.pop()
	while v_now > L:
		if que[0][1] <= v_now - L:
			v_now -= que[0][1]
			vt_now -= que[0][1] * que[0][0]
			que.popleft()
		else:
			que[0][1] -= v_now - L
			vt_now -= (v_now - L) * que[0][0]
			v_now = L
	TT_list.append(vt_now / L)
for i in range(N):
	print(TT_list[i])