A new strain of flu has broken out. Fortunately, a vaccine was developed very quickly and is now being administered to the public. Your local health clinic is administering this vaccine, but the waiting line is very long.

For safety reasons, people are not allowed to stand very close to each other as the flu is not under control yet. However, many people were not aware of this precaution. A health and safety official recently examined the line and has determined that people need to spread out more in the line so that they are at least T units away from each other. This needs to be done as quickly as possible so we need to calculate the minimum distance D such that it is possible for every person to move at most D units so the distance between any two people is at least T. Specifically, D should be the minimum value such that there are locations x'_{i} so that |x_{i} - x'_{i}| ≤ D for each person i and |x'_{i} - x'_{j}| ≥ T for any two distinct people i,j. Furthermore, since nobody can move past the receptionist we must also have that x'_{i} ≥ 0. 

The location of each person is given by the number of meters they are standing from the receptionist. When spreading out, people may move either forward or backward in line but nobody may move past the location of the receptionist.

------ Input ------ 

The first line of input contains a single integer K ≤ 30 indicating the number of test cases to follow. Each test case begins with a line containing an integer N (the number of people) and a floating point value T (the minimum distance that should be between people). The location of each person i is described by single floating point value x_{i} which means person i is x_{i} meters from the receptionist. These values appear in non-decreasing order on the following N lines, one value per line.

Bounds: 1 ≤ N ≤ 10,000 and T and every x_{i} is between 0 and 1,000,000 and is given with at most 3 decimals of precision.

------ Output ------ 

For each test case, you should output the minimum value of D with exactly 4 decimals of precision on a single line.

----- Sample Input 1 ------ 
3
2 4
1
2
2 2
1
2
4 1
0
0.5
0.6
2.75
----- Sample Output 1 ------ 
2.0000
0.5000
1.4000
----- explanation 1 ------ 
Test case $1$: To maintain a distance of $4$ units, the first person can move to location $0$ and the second can move to location $4$. The maximum distance a person has to move is $2$.

Test case $2$: To maintain a distance of $2$ units, the first person can move to location $0.5$ and the second person can move to location $2.5$. The maximum distance a person has to move is $0.5$. 

Test case $3$: To maintain a distance of $1$ unit, the first person does not move, the second moves to location $1$, the third moves to location $2$, and the fourth moves to location $3$. The corresponding distances moved by each of them is $0, 0,5, 1.4,$ and $0.25$ respectively. Thus, the maximum distance moved by any person is $1.4$ moved by the third person.

Source: codechef | Difficulty: MEDIUM_HARD
Tags: Sorting

Complete search


Solutions:
def check(x, n, k, mid):
	curr = x[n - 1] + mid
	for i in range(n - 2, -1, -1):
		curr -= k
		if curr > x[i] + mid:
			curr = x[i] + mid
		elif curr < x[i] - mid:
			return False
	if curr >= 0.0:
		return True
	else:
		return False
for _ in range(int(input())):
	(n, k) = map(float, input().split(' '))
	n = int(n)
	x = []
	for i in range(n):
		pos = float(input())
		x.append(pos)
	(low, high, ans) = (0.0, 10000000.0, 0.0)
	while high - low > 5e-05:
		mid = (low + high) / 2.0
		if check(x, n, k, mid) == True:
			ans = mid
			high = mid
		else:
			low = mid
	print('%.4f' % ans)


tc = int(input())
for k in range(tc):
	(n, t) = input().split()
	(n, t) = (int(n), float(t))
	arr = []
	for j in range(n):
		arr.append(float(input()))
	l = 0.0
	r = 10.0 ** 12
	mn = r
	while True:
		mid = l + (r - l) / 2
		if r - l <= 0.0001:
			mn = mid
			break
		right = None
		flag = True
		for i in range(n - 1, -1, -1):
			if i == n - 1:
				right = arr[n - 1] + mid - t
			elif arr[i] <= right:
				pos = min(arr[i] + mid, right)
				right = pos - t
			else:
				pos = max(arr[i] - mid, right)
				if pos > right or pos < 0:
					flag = False
					break
				right = pos - t
		if flag == True:
			r = mid
		else:
			l = mid
	num = str(round(mn, 4)).split('.')
	while len(num[1]) < 4:
		num[1] += '0'
	print('.'.join(num))


def check(dist):
	cur = a[n - 1] + dist
	for i in range(n - 2, -1, -1):
		cur -= t
		if cur > a[i] + dist:
			cur = a[i] + dist
		elif cur < a[i] - dist:
			return False
	if cur >= 0:
		return True
	return False
k = int(input())
for _ in range(k):
	(n, t) = map(float, input().split())
	n = int(n)
	a = []
	for _ in range(n):
		num = float(input())
		a.append(num)
	(lo, hi) = (0, 10 ** 10)
	while hi - lo > 5e-05:
		mid = (lo + hi) / 2
		if check(mid):
			ans = mid
			hi = mid
		else:
			lo = mid
	print(f'{ans:.4f}')


def check(arr, n, t, mid):
	curr = arr[n - 1] + mid
	for i in range(n - 2, -1, -1):
		curr -= t
		if curr > arr[i] + mid:
			curr = arr[i] + mid
		elif curr < arr[i] - mid:
			return False
	if curr >= 0.0:
		return True
	else:
		return False

def solve(arr, n, t):
	(low, high, ans) = (0.0, 10000000000, 0.0)
	while high - low > 5e-05:
		mid = (low + high) / 2
		if check(arr, n, t, mid) == True:
			ans = mid
			high = mid
		else:
			low = mid
	return '%.4f' % ans
for i in range(int(input())):
	(n, t) = [float(x) for x in input().split()]
	n = int(n)
	arr = []
	giv = []
	for i in range(n):
		r = float(input())
		arr.append(r)
	print(solve(arr, n, t))


def check(p, n, t, mid):
	curr = p[n - 1] + mid
	for i in range(n - 2, -1, -1):
		curr -= t
		if curr > p[i] + mid:
			curr = p[i] + mid
		elif curr < p[i] - mid:
			return False
	if curr >= 0.0:
		return True
	else:
		return False

def solve(p, n, t):
	(low, high, ans) = (0.0, 100000000000.0, 0.0)
	while high - low > 5e-05:
		mid = (low + high) / 2.0
		if check(p, n, t, mid):
			ans = mid
			high = mid
		else:
			low = mid
	print('%.4f' % ans)
t = int(input())
for _ in range(t):
	(n, k) = map(float, input().split())
	n = int(n)
	v = []
	for i in range(n):
		pos = float(input())
		v.append(pos)
	solve(v, n, k)


def ok(d):
	lst = max(0, x[0] - d)
	for i in range(1, n):
		if x[i] + d < lst + t:
			return False
		else:
			lst = max(x[i] - d, lst + t)
	return True
tst = int(input())
for _ in range(tst):
	(n, t) = map(float, input().split(' '))
	n = int(n)
	x = []
	for i in range(n):
		p = float(input())
		x.append(p)
	l = 0.0
	r = 1000000000.0
	for it in range(1, 60):
		mid = (l + r) / 2.0
		if ok(mid) == True:
			r = mid
		else:
			l = mid
	print('%.4f' % l)


def check(positions, N, T, mid):
	currPos = positions[N - 1] + mid
	for i in range(N - 2, -1, -1):
		currPos -= T
		if currPos > positions[i] + mid:
			currPos = positions[i] + mid
		elif currPos < positions[i] - mid:
			return False
	if currPos >= 0.0:
		return True
	return False

def solve(positions, N, T):
	(low, high, ans) = (0.0, 10000000000.0, 0.0)
	while high - low > 5e-05:
		mid = (low + high) / 2.0
		if check(positions, N, T, mid) == True:
			ans = mid
			high = mid
		else:
			low = mid
	print('%.4f' % ans)
T = int(input())
for _ in range(T):
	(N, K) = map(float, input().split(' '))
	N = int(N)
	A = []
	for i in range(N):
		pos = float(input())
		A.append(pos)
	solve(A, N, K)