Building Bridge (Codeforces 250D)
Tags: Others, ternary search

Two villages are separated by a river that flows from the north to the south. The villagers want to build a bridge across the river to make it easier to move across the villages.The river banks can be assumed to be vertical straight lines x = a and x = b (0 < a < b).The west village lies in a steppe at point O = (0, 0). There are n pathways leading from the village to the river, they end at points Ai = (a, yi). The villagers there are plain and simple, so their pathways are straight segments as well.The east village has reserved and cunning people. Their village is in the forest on the east bank of the river, but its exact position is not clear. There are m twisted paths leading from this village to the river and ending at points Bi = (b, y'i). The lengths of all these paths are known, the length of the path that leads from the eastern village to point Bi, equals li.The villagers want to choose exactly one point on the left bank of river Ai, exactly one point on the right bank Bj and connect them by a straight-line bridge so as to make the total distance between the villages (the sum of |OAi| + |AiBj| + lj, where |XY| is the Euclidean distance between points X and Y) were minimum. The Euclidean distance between points (x1, y1) and (x2, y2) equals .Help them and find the required pair of points.

Input: The first line contains integers n, m, a, b (1 ≤ n, m ≤ 105, 0 < a < b < 106). The second line contains n integers in the ascending order: the i-th integer determines the coordinate of point Ai and equals yi (|yi| ≤ 106). The third line contains m integers in the ascending order: the i-th integer determines the coordinate of point Bi and equals y'i (|y'i| ≤ 106). The fourth line contains m more integers: the i-th of them determines the length of the path that connects the eastern village and point Bi, and equals li (1 ≤ li ≤ 106).It is guaranteed, that there is such a point C with abscissa at least b, that |BiC| ≤ li for all i (1 ≤ i ≤ m). It is guaranteed that no two points Ai coincide. It is guaranteed that no two points Bi coincide.

Output: Print two integers — the numbers of points on the left (west) and right (east) banks, respectively, between which you need to build a bridge. You can assume that the points on the west bank are numbered from 1 to n, in the order in which they are given in the input. Similarly, the points on the east bank are numbered from 1 to m in the order in which they are given in the input.If there are multiple solutions, print any of them. The solution will be accepted if the final length of the path will differ from the answer of the jury by no more than 10 - 6 in absolute or relative value.


Sample Inputs:
3 2 3 5
-2 -1 4
-1 2
7 3

Sample Outputs:
2 2

Solution (Python):
import math
(n, m, a, b) = map(int, input().split())
A = list(map(int, input().strip().split()))
B = list(map(int, input().strip().split()))
D = list(map(int, input().strip().split()))
j = -1
r = (1e+100, -1, -1)
for i in range(m):
    while not ((j == -1 or a * B[i] - b * A[j] >= 0) and (j + 1 == n or a * B[i] - b * A[j + 1] < 0)):
        j += 1
    if j != -1:
        r = min(r, (D[i] + math.sqrt(a * a + A[j] * A[j]) + math.sqrt((b - a) ** 2 + (B[i] - A[j]) ** 2), j, i))
    if j + 1 != n:
        r = min(r, (D[i] + math.sqrt(a * a + A[j + 1] * A[j + 1]) + math.sqrt((b - a) ** 2 + (B[i] - A[j + 1]) ** 2), j + 1, i))
print(r[1] + 1, r[2] + 1)

Solution (Python):
import sys
from math import *

def minp():
    return sys.stdin.readline().strip()

def mint():
    return int(minp())

def mints():
    return map(int, minp().split())
(n, m, a, b) = mints()
A = list(mints())
B = list(mints())
l = list(mints())
j = -1
r = (1e+100, -1, -1)
for i in range(m):
    while not ((j == -1 or a * B[i] - b * A[j] >= 0) and (j + 1 == n or a * B[i] - b * A[j + 1] < 0)):
        j += 1
    if j != -1:
        r = min(r, (l[i] + sqrt(a * a + A[j] * A[j]) + sqrt((b - a) ** 2 + (B[i] - A[j]) ** 2), j, i))
    if j + 1 != n:
        r = min(r, (l[i] + sqrt(a * a + A[j + 1] * A[j + 1]) + sqrt((b - a) ** 2 + (B[i] - A[j + 1]) ** 2), j + 1, i))
print(r[1] + 1, r[2] + 1)

Solution (Python):
import sys
from itertools import *
from math import *

def solve():
    (n, m, leftbank, rightbank) = map(int, input().split())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    l = list(map(int, input().split()))
    smallx = leftbank
    smallxsquared = smallx * smallx
    rightbankminusleftbanksquared = (rightbank - leftbank) * (rightbank - leftbank)
    (leftbest, rightbest, distbest) = (-1, -1, 100000000)
    ll = 0
    for (i, bcord, length) in zip(count(), b, l):
        wanty = bcord * smallx / rightbank
        while ll < n and a[ll] <= wanty:
            ll += 1
        for pos in range(ll - 1, ll + 1):
            if pos >= 0 and pos < n:
                first = sqrt(smallxsquared + a[pos] * a[pos])
                second = sqrt(rightbankminusleftbanksquared + (bcord - a[pos]) * (bcord - a[pos]))
                totaldist = first + second + length
                if totaldist < distbest:
                    distbest = totaldist
                    leftbest = pos
                    rightbest = i
    print(leftbest + 1, rightbest + 1)
if sys.hexversion == 50594544:
    sys.stdin = open('test.txt')
solve()