Water Taps (Codeforces 954E)
Tags: Greedy, sortings

Consider a system of n water taps all pouring water into the same container. The i-th water tap can be set to deliver any amount of water from 0 to ai ml per second (this amount may be a real number). The water delivered by i-th tap has temperature ti.If for every  you set i-th tap to deliver exactly xi ml of water per second, then the resulting temperature of water will be  (if , then to avoid division by zero we state that the resulting water temperature is 0).You have to set all the water taps in such a way that the resulting temperature is exactly T. What is the maximum amount of water you may get per second if its temperature has to be T?

Input: The first line contains two integers n and T (1 ≤ n ≤ 200000, 1 ≤ T ≤ 106) — the number of water taps and the desired temperature of water, respectively.The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 106) where ai is the maximum amount of water i-th tap can deliver per second.The third line contains n integers t1, t2, ..., tn (1 ≤ ti ≤ 106) — the temperature of water each tap delivers.

Output: Print the maximum possible amount of water with temperature exactly T you can get per second (if it is impossible to obtain water with such temperature, then the answer is considered to be 0).Your answer is considered correct if its absolute or relative error doesn't exceed 10 - 6.


Sample Inputs:
2 100
3 10
50 150

3 9
5 5 30
6 6 10

2 12
1 3
10 15

Sample Outputs:
6.000000000000000

40.000000000000000

1.666666666666667

Solution (Python):
import sys
[n, T] = list(map(int, sys.stdin.readline().strip().split()))
ais = list(map(int, sys.stdin.readline().strip().split()))
tis = list(map(int, sys.stdin.readline().strip().split()))
i_0 = [i for i in range(n) if tis[i] == T]
i_hot = [i for i in range(n) if tis[i] > T]
i_cold = [i for i in range(n) if tis[i] < T]
tis = [abs(ti - T) for ti in tis]
w_hot = sum((ais[i] * tis[i] for i in i_hot))
w_cold = sum((ais[i] * tis[i] for i in i_cold))
if w_hot > w_cold:
    (w_hot, w_cold) = (w_cold, w_hot)
    (i_hot, i_cold) = (i_cold, i_hot)
x_max = sum((ais[i] for i in i_0)) + sum((ais[i] for i in i_hot))
w = w_hot
i_cold.sort(key=lambda _k: tis[_k])
for i in i_cold:
    (a, t) = (ais[i], tis[i])
    if a * t <= w:
        w -= a * t
        x_max += a
    else:
        x_max = x_max + float(w) / t
        w = 0
        break
print(float(x_max))

Solution (Python):
import sys
readline = sys.stdin.readline
readlines = sys.stdin.readlines
ns = lambda : readline().rstrip()
ni = lambda : int(readline().rstrip())
nm = lambda : map(int, readline().split())
nl = lambda : list(map(int, readline().split()))
prn = lambda x: print(*x, sep='\n')

def solve():
    (n, T) = nm()
    a = nl()
    t = nl()
    if min(t) > T or max(t) < T:
        print(0)
        return
    g = [(t[i] - T, i) for i in range(n)]
    h = sum(((t[i] - T) * a[i] for i in range(n)))
    ans = sum(a)
    if h < 0:
        g.sort()
        for (x, i) in g:
            if h < x * a[i]:
                ans -= a[i]
                h -= x * a[i]
            else:
                if h and x != 0:
                    ans -= h / x
                break
    elif h > 0:
        g.sort(reverse=True)
        for (x, i) in g:
            if h > x * a[i]:
                ans -= a[i]
                h -= x * a[i]
            else:
                if h and x != 0:
                    ans -= h / x
                break
    print(ans)
    return
solve()

Solution (Python):
def get_max_volume(sources, required_temperature):
    """"""
    max_volume = 0.0
    temp = 0
    higher_sources = []
    lower_sources = []
    for (volume, temperature) in sources:
        delta_temp = temperature - required_temperature
        if delta_temp > 0:
            higher_sources.append((volume, delta_temp))
        elif delta_temp < 0:
            lower_sources.append((volume, delta_temp))
        max_volume += volume
        temp += volume * delta_temp
    higher_sources.sort(key=lambda v: v[1])
    lower_sources.sort(key=lambda v: -v[1])
    while abs(temp / max_volume) >= 1e-06 and (len(lower_sources) > 0 or temp >= 0) and (len(higher_sources) > 0 or temp <= 0):
        if temp < 0:
            (volume, delta_temp) = lower_sources.pop()
            if temp - delta_temp * volume >= 0:
                required_volume = temp / delta_temp
                return max_volume - required_volume
            temp -= delta_temp * volume
            max_volume -= volume
        else:
            (volume, delta_temp) = higher_sources.pop()
            if temp - delta_temp * volume <= 0:
                required_volume = temp / delta_temp
                return max_volume - required_volume
            temp -= delta_temp * volume
            max_volume -= volume
    if abs(temp / max_volume) < 1e-06:
        return max_volume
    return 0.0
(n, t) = map(int, input().split())
vs = input().split()
ts = input().split()
ss = [(int(vs[i]), int(ts[i])) for i in range(n)]
print(get_max_volume(ss, t))