Discrete Acceleration (Codeforces 1408C)
Tags: Math, two pointers

There is a road with length $$$l$$$ meters. The start of the road has coordinate $$$0$$$, the end of the road has coordinate $$$l$$$.There are two cars, the first standing at the start of the road and the second standing at the end of the road. They will start driving simultaneously. The first car will drive from the start to the end and the second car will drive from the end to the start.Initially, they will drive with a speed of $$$1$$$ meter per second. There are $$$n$$$ flags at different coordinates $$$a_1, a_2, \ldots, a_n$$$. Each time when any of two cars drives through a flag, the speed of that car increases by $$$1$$$ meter per second.Find how long will it take for cars to meet (to reach the same coordinate). 

Input: The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$): the number of test cases.The first line of each test case contains two integers $$$n$$$, $$$l$$$ ($$$1 \leq n \leq 10^5$$$, $$$1 \leq l \leq 10^9$$$): the number of flags and the length of the road.The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ in the increasing order ($$$1 \leq a_1 < a_2 < \ldots < a_n < l$$$).It is guaranteed that the sum of $$$n$$$ among all test cases does not exceed $$$10^5$$$.

Output: For each test case print a single real number: the time required for cars to meet.Your answer will be considered correct, if its absolute or relative error does not exceed $$$10^{-6}$$$. More formally, if your answer is $$$a$$$ and jury's answer is $$$b$$$, your answer will be considered correct if $$$\frac{|a-b|}{\max{(1, b)}} \leq 10^{-6}$$$.

Note: In the first test case cars will meet in the coordinate $$$5$$$.The first car will be in the coordinate $$$1$$$ in $$$1$$$ second and after that its speed will increase by $$$1$$$ and will be equal to $$$2$$$ meters per second. After $$$2$$$ more seconds it will be in the coordinate $$$5$$$. So, it will be in the coordinate $$$5$$$ in $$$3$$$ seconds.The second car will be in the coordinate $$$9$$$ in $$$1$$$ second and after that its speed will increase by $$$1$$$ and will be equal to $$$2$$$ meters per second. After $$$2$$$ more seconds it will be in the coordinate $$$5$$$. So, it will be in the coordinate $$$5$$$ in $$$3$$$ seconds.In the second test case after $$$1$$$ second the first car will be in the coordinate $$$1$$$ and will have the speed equal to $$$2$$$ meters per second, the second car will be in the coordinate $$$9$$$ and will have the speed equal to $$$1$$$ meter per second. So, they will meet after $$$\frac{9-1}{2+1} = \frac{8}{3}$$$ seconds. So, the answer is equal to $$$1 + \frac{8}{3} = \frac{11}{3}$$$.

Sample Inputs:
5
2 10
1 9
1 10
1
5 7
1 2 3 4 6
2 1000000000
413470354 982876160
9 478
1 10 25 33 239 445 453 468 477

Sample Outputs:
3.000000000000000
3.666666666666667
2.047619047619048
329737645.750000000000000
53.700000000000000

Solution (Python):
import bisect
import collections
import random
import sys
p2D = lambda x: print(*x, sep='\n')

def II():
    return int(sys.stdin.buffer.readline())

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

def LI():
    return list(map(int, sys.stdin.buffer.readline().split()))

def LLI(rows_number):
    return [LI() for _ in range(rows_number)]

def BI():
    return sys.stdin.buffer.readline().rstrip()

def SI():
    return sys.stdin.buffer.readline().rstrip().decode()

def li():
    return [int(i) for i in input().split()]

def lli(rows):
    return [li() for _ in range(rows)]

def si():
    return input()

def ii():
    return int(input())

def ins():
    return input().split()

def drank(d, processing, da, rank):
    tmp = 10 ** 9
    if len(d[da]) == 1:
        return 1
    for di in d[da]:
        if processing[di - 1] == 0:
            processing[di - 1] = 1
            tmp = min(tmp, drank(d, processing, di, rank))
            processing[di - 1] = 0
    rank[da - 1] = tmp + 1
    return tmp + 1

def binary_search(n, a):
    l = len(a)
    low = 0
    high = l - 1
    while high >= low:
        mid = (high + low) // 2
        if a[mid] == n:
            return mid
        elif a[mid] > n:
            high = mid - 1
        else:
            low = mid + 1
    return -1

def solve():
    (n, l) = LI()
    a = [0] + LI() + [l]
    sp = 1
    tf = [0]
    ts = [0]
    for i in range(1, n + 2):
        tf.append(tf[-1] + (a[i] - a[i - 1]) / sp)
        ts.append(ts[-1] + (a[n - i + 2] - a[n - i + 1]) / sp)
        sp += 1
    ts.reverse()
    for i in range(n + 2):
        if tf[i] == ts[i]:
            return ts[i]
        if tf[i] > ts[i]:
            if tf[i - 1] <= ts[i]:
                return ts[i] + (a[i] - a[i - 1] - (ts[i] - tf[i - 1]) * i) / (n + 2)
            else:
                return tf[i - 1] + (a[i] - a[i - 1] - (tf[i - 1] - ts[i]) * (n - i + 2)) / (n + 2)

def main():
    for _ in range(II()):
        sys.stdout.write(str(solve()) + '\n')
if __name__ == '__main__':
    main()

Solution (Python):
for _ in range(int(input())):
    (n, l) = map(int, input().split())
    A = list(map(int, input().split()))
    (ca, cb) = (0, l)
    (sa, sb) = (1, 1)
    (i, j) = (0, n - 1)
    ans = 0
    while i <= j:
        da = A[i] - ca
        db = cb - A[j]
        ta = da / sa
        tb = db / sb
        if ta <= tb:
            ans += ta
            ca += da
            cb -= sb * ta
            sa += 1
            i += 1
        else:
            ans += tb
            cb -= db
            ca += sa * tb
            sb += 1
            j -= 1
    d = cb - ca
    s = sa + sb
    t = d / s
    ans += t
    print('{:6f}'.format(ans))

Solution (Python):
from threading import Thread, stack_size
from collections import deque
from sys import stdin, setrecursionlimit
input = stdin.buffer.readline

def target():

    def func(l, r, first, second, arr):
        if not arr:
            return (r - l) / (first + second)
        time1 = (arr[0] - l) / first
        time2 = (r - arr[-1]) / second
        ele1 = arr[0]
        ele2 = arr[-1]
        if time1 < time2:
            arr.popleft()
            return func(ele1, r - second * time1, first + 1, second, arr) + time1
        else:
            arr.pop()
            return func(l + time2 * first, ele2, first, second + 1, arr) + time2
    for _ in range(int(input())):
        (n, l) = map(int, input().split())
        a = deque(map(int, input().split()))
        print(func(0, l, 1, 1, a))
setrecursionlimit(10 ** 5 + 100)
stack_size(10 ** 8)
Thread(target=target).start()