Similar statements (1)

Consider two infinite horizontal lines A and B, separated ℓ units apart.
The line A has m points at the abscissae a₁, …, a_(m). The line B has n
points at the abscissae b₁, …, b_(n). Given p different indices
i₁, …, i_(p) choosen from {1…m}, and p different indices j₁, …, j_(p)
choosen from {1…n}, define d_(k) as the Euclidean distance between
a_(i_(k)) and b_(j_(k)), that is,
$$d_k = \sqrt{(a_{i_k} - b_{j_k})^2 + \ell^2}
\enspace .$$

You are given ℓ, p, and the points in A and in B. Pick i₁, …, i_(p) and
j₁, …, j_(p) in order to

minimize ∑_(k = 1..p) d_(k)

Input

Input consists of several cases, each one with only integer numbers.
Every case begins with four strictly positive numbers ℓ, p, m and n.
Follow a₁ ≤ a₂ ≤ … ≤ a_(m − 1) ≤ a_(m). Follow
b₁ ≤ b₂ ≤ … ≤ b_(n − 1) ≤ b_(n). Assume ℓ ≤ 10⁶, p ≤ min (m, n), and
that the absolute value of each abscissa is at most 10⁶.

Additionally, assume that m and n are at most 1000.

Output

For every case, print the result with four digits after the decimal
point. If you use the long double type, the input cases have no
precision issues.

Problem information

Author: Salvador Roura

Generation: 2026-01-25T11:36:20.285Z

© Jutge.org, 2006–2026.
https://jutge.org
