Problem: Along a road running in an east-west direction, there are A shrines and B temples. The i-th shrine from the west is located at a distance of s_i meters from the west end of the road, and the i-th temple from the west is located at a distance of t_i meters from the west end of the road.  Answer the following Q queries:    * Query i (1 \leq i \leq Q): If we start from a point at a distance of x_i meters from the west end of the road and freely travel along the road, what is the minimum distance that needs to be traveled in order to visit one shrine and one temple? (It is allowed to pass by more shrines and temples than required.)

Submission attempts (in order):
import numpy as np
A, B, Q = map(int, input().split())

s_list = [int(input()) for a in range(A)]
t_list = [int(input()) for b in range(B)]
x_list = [int(input()) for q in range(Q)]

for x in x_list:
    near_s = np.abs(np.array(s_list) - x).argmin()
    near_t = np.abs(np.array(t_list) - x).argmin()

    if s_list[near_s] < t_list[near_t]:
        next_x = s_list[near_s]
        near_t2 = np.abs(np.array(t_list) - next_x).argmin()
        print(abs(x - s_list[near_s] + abs(next_x - t_list[near_t2])))

    elif s_list[near_s] > t_list[near_t]:
        next_x = t_list[near_t]
        near_s2 = np.abs(np.array(s_list) - next_x).argmin()
        print(abs(x - t_list[near_t] + abs(next_x - s_list[near_s2])))
    else:
        next_x1 = s_list[near_s]
        next_x2 = t_list[near_t]
        near_t2 = np.abs(np.array(t_list) - next_x).argmin()
        near_s2 = np.abs(np.array(s_list) - next_x).argmin()
        if abs(x - s_list[near_s] + abs(next_x - t_list[near_t2])) < abs(x - t_list[near_t] + abs(next_x - s_list[near_s2])):
            print(abs(x - s_list[near_s] + abs(next_x - t_list[near_t2])))
        else:
            print(abs(x - t_list[near_t] + abs(next_x - s_list[near_s2])))

A, B, Q = map(int, input().split())
s_list = [int(input()) for a in range(A)]
t_list = [int(input()) for b in range(B)]
x_list = [int(input()) for q in range(Q)]

for x in x_list:
    s_distance = min(list(map(lambda n: abs(n - x), s_list)))
    s_index = list(map(lambda n: abs(n - x), s_list)).index(s_distance)
    t_distance = min(list(map(lambda n: abs(n - x), t_list)))
    t_index = list(map(lambda n: abs(n - x), t_list)).index(t_distance)
    if s_distance < t_distance:
        next_x = s_list[s_index]
        t_distance_2 = min(list(map(lambda n: abs(n - next_x), t_list)))
        print(s_distance + t_distance_2)
    elif s_distance > t_distance:
        next_x = t_list[t_index]
        s_distance_2 = min(list(map(lambda n: abs(n - next_x), s_list)))
        print(t_distance + s_distance_2)
    else:
        next_x_1 = s_list[s_index]
        next_x_2 = t_list[t_index]
        s_distance_2 = min(list(map(lambda n: abs(n - next_x_2), s_list)))
        t_distance_2 = min(list(map(lambda n: abs(n - next_x_1), t_list)))
        if s_distance + t_distance_2 < s_distance_2 + t_distance:
            print(s_distance + t_distance_2)
        else:
            print(t_distance + s_distance_2)

from bisect import bisect

a,b,q=map(int,input().split())
INF=10**18
s=[-INF]+[int(input()) for i in range(a)]+[INF]
t=[-INF]+[int(input()) for i in range(b)]+[INF]

for i in range(q):
  x=int(input())
  s_inx=bisect(s,x)
  t_inx=bisect(t,x)

  s_l=s[s_inx-1]
  s_r=s[s_inx]

  t_l=t[t_inx-1]
  t_r=t[t_inx]

  ans=10**18
  for i in [s_l,s_r]:
    for j in [t_l,t_r]:
      d1=abs(i-x)+abs(j-i)
      d2=abs(j-x)+abs(i-j)
      ans=min(ans,d1,d2)

  print(ans)

Verdicts: Wrong Answer

Wrong Answer

Accepted