Weakness and Poorness (Codeforces 579E)
Tags: Others, ternary search

You are given a sequence of n integers a1, a2, ..., an. Determine a real number x such that the weakness of the sequence a1 - x, a2 - x, ..., an - x is as small as possible.The weakness of a sequence is defined as the maximum value of the poorness over all segments (contiguous subsequences) of a sequence.The poorness of a segment is defined as the absolute value of sum of the elements of segment.

Input: The first line contains one integer n (1 ≤ n ≤ 200 000), the length of a sequence.The second line contains n integers a1, a2, ..., an (|ai| ≤ 10 000).

Output: Output a real number denoting the minimum possible weakness of a1 - x, a2 - x, ..., an - x. Your answer will be considered correct if its relative or absolute error doesn't exceed 10 - 6.

Note: For the first case, the optimal value of x is 2 so the sequence becomes  - 1, 0, 1 and the max poorness occurs at the segment "-1" or segment "1". The poorness value (answer) equals to 1 in this case. For the second sample the optimal value of x is 2.5 so the sequence becomes  - 1.5,  - 0.5, 0.5, 1.5 and the max poorness occurs on segment "-1.5 -0.5" or "0.5 1.5". The poorness value (answer) equals to 2 in this case.

Sample Inputs:
3
1 2 3

4
1 2 3 4

10
1 10 2 9 3 8 4 7 5 6

Sample Outputs:
1.000000000000000

2.000000000000000

4.500000000000000

Solution (Python):
def max_sum(nums, shift):
    res = 0
    res_m = 0
    cur_sum = 0
    cur_m_sum = 0
    for i in range(len(nums)):
        cur_sum += nums[i] + shift
        cur_m_sum += nums[i] + shift
        res = max(res, cur_sum)
        cur_sum = max(0, cur_sum)
        res_m = min(res_m, cur_m_sum)
        cur_m_sum = min(0, cur_m_sum)
    return (res, -res_m)

def weaks(nums, shift):
    return max_sum(nums, shift)

def main():
    int(input())
    nums = list(map(int, input().split()))
    l = -10000
    r = 10000
    ans = max(weaks(nums, 0))
    w1 = 1
    w2 = -1
    PREC = 10 ** (-6)
    while abs(w1 - w2) >= PREC and abs(w1 - w2) > PREC * max(w1, w2):
        m = (r + l) / 2
        (w1, w2) = weaks(nums, m)
        if w1 > w2:
            r = m
        else:
            l = m
    print((w1 + w2) / 2)
main()

Solution (Python):
def cal(x, li, sign):
    sum = 0
    ans = 0
    for i in range(0, n):
        sum += (li[i] - x) * sign
        if sum < 0:
            sum = 0
        if sum > ans:
            ans = sum
    return ans
n = int(input())
li = [int(x) for x in input().split()]
r = max(li)
l = min(li)
ans = -1
mn = 99999999
for i in range(30):
    k = (r + l) / 2
    a1 = cal(k, li, 1)
    a2 = cal(k, li, -1)
    if abs(a1) < abs(a2):
        r = k
    else:
        l = k
    if abs(abs(a1) - abs(a2)) < mn:
        mn = abs(abs(a1) - abs(a2))
        ans = abs(a1)
        if abs(a2) > abs(a1):
            ans = abs(a1)
    if r - l < 1e-06:
        break
print(ans)

Solution (Python):
def max_sum(x):
    ans = now = 0
    for v in a:
        now += v - x
        now = max(now, 0)
        ans = max(ans, now)
    return ans

def min_sum(x):
    ans = now = 0
    for v in a:
        now += v - x
        now = min(now, 0)
        ans = min(ans, now)
    return ans
n = int(input())
a = [int(x) for x in input().split()]
l = min(a)
r = max(a)
for _ in range(50):
    x = (r + l) / 2
    A = abs(max_sum(x))
    B = abs(min_sum(x))
    if A < B:
        r = x
    else:
        l = x
print(max(A, B))