Problem:


Score : 400 points

Problem Statement

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.)

Constraints

1 \leq A, B \leq 10^5

1 \leq Q \leq 10^5

1 \leq s_1 < s_2 < ... < s_A \leq 10^{10}

1 \leq t_1 < t_2 < ... < t_B \leq 10^{10}

1 \leq x_i \leq 10^{10}

s_1, ..., s_A, t_1, ..., t_B, x_1, ..., x_Q are all different.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B Q
s_1
:
s_A
t_1
:
t_B
x_1
:
x_Q

Output

Print Q lines. The i-th line should contain the answer to the i-th query.

Sample Input 1

2 3 4
100
600
400
900
1000
150
2000
899
799

Sample Output 1

350
1400
301
399

There are two shrines and three temples. The shrines are located at distances of 100, 600 meters from the west end of the road, and the temples are located at distances of 400, 900, 1000 meters from the west end of the road.

Query 1: If we start from a point at a distance of 150 meters from the west end of the road, the optimal move is first to walk 50 meters west to visit a shrine, then to walk 300 meters east to visit a temple.

Query 2: If we start from a point at a distance of 2000 meters from the west end of the road, the optimal move is first to walk 1000 meters west to visit a temple, then to walk 400 meters west to visit a shrine. We will pass by another temple on the way, but it is fine.

Query 3: If we start from a point at a distance of 899 meters from the west end of the road, the optimal move is first to walk 1 meter east to visit a temple, then to walk 300 meters west to visit a shrine.

Query 4: If we start from a point at a distance of 799 meters from the west end of the road, the optimal move is first to walk 199 meters west to visit a shrine, then to walk 200 meters west to visit a temple.

Sample Input 2

1 1 3
1
10000000000
2
9999999999
5000000000

Sample Output 2

10000000000
10000000000
14999999998

The road is quite long, and we may need to travel a distance that does not fit into a 32-bit integer.



Sample input:
2 3 4
100
600
400
900
1000
150
2000
899
799


Sample output:
350
1400
301
399

Submission status: Wrong Answer

Solution code:
# input
a, b, q = map(int, input().split())
S = [int(input()) for _ in range(a)]
T = [int(input()) for _ in range(b)]

S_rinsetsu = [0] * a
tmp = 0

for i in range(a):
    for j in range(tmp, b):
        if S[i] <= T[j]:
            S_rinsetsu[i] = j
            tmp = j
            break

for k in range(q):
    x = int(input())
    for i in range(a):
        if x <= S[i]:
            if i == 0:
                ans = max(S[0] - x, T[0] - x)
                print(ans)
                break
            elif x <= T[0]:
                ans = min(max(S[i] - x, T[0] - x), T[0] - x + T[0] - S[i - 1], x - S[i - 1] + T[0] - S[i - 1])
                print(ans)
                break
            else:
                if x > T[S_rinsetsu[i] - 1]:
                    ans1 = min(S[i] - x, x - T[S_rinsetsu[i] - 1]) + S[i] - T[S_rinsetsu[i] - 1]
                else:
                    ans1 = S[i] - x
                ans2 = T[S_rinsetsu[i]]
                ans3 = x - T[S_rinsetsu[i - 1] - 1]
                if x < T[S_rinsetsu[i]]:
                    ans4 = min(T[S_rinsetsu[i - 1]] - x, x - S[i - 1]) + T[S_rinsetsu[i - 1]] - S[i - 1]
                else:
                    ans4 = x - S[i - 1]
                ans = min(ans1, ans2, ans3, ans4)
                print(ans)
                break
        if i == a - 1:
            if T[S_rinsetsu[i]] > x:
                ans = min(T[S_rinsetsu[i]] - x + T[S_rinsetsu[i]] - S[i], x - S[i] + T[S_rinsetsu[i]] - S[i], x - T[S_rinsetsu[i] - 1])
                print(ans)
            else:
                ans = x - S[i]
                print(ans)

Submission status: Wrong Answer

Solution code:
import bisect

A, B, Q = map(int, input().split())
S = [int(input()) for _ in range(A)]
T = [int(input()) for _ in range(B)]

def nn(a, x):
    i = bisect.bisect_left(a, x)
    if i == 0:
        return a[0], a[0]
    if i == len(a):
        return a[i-1], a[i-1]
    if abs(a[i-1]-x) == abs(a[i]-x):
        return a[i-1], a[i]
    if abs(a[i-1]-x) < abs(a[i]-x):
        return a[i-1], a[i-1]
    return a[i], a[i]

for _ in range(Q):
    x = int(input())
    s1, s2 = nn(S, x)
    t1, t2 = nn(T, x)
    st1, _ = nn(T, s1)
    st2, _ = nn(T, s2)
    ts1, _ = nn(S, t1)
    ts2, _ = nn(S, t2)
    print(
        min(
            abs(x-s1) + abs(s1-st1),
            abs(x-t1) + abs(t1-ts1),
            abs(x-s2) + abs(s2-st2),
            abs(x-t2) + abs(t2-ts2)
        )
    )

Submission status: Wrong Answer

Solution code:
import bisect
import numpy as np

A, B, Q = [int(i) for i in input().split()]
s = np.array([int(input()) for i in [0]*A])
t = np.array([int(input()) for i in [0]*B])
x = [int(input()) for i in [0]*Q]
ls = []

for q in x:
    a = s-q
    b = t-q
    S = np.abs(a)
    T = np.abs(b)
    if min(S)<=min(T):
        c = b - a[np.argmin(S)]
        U = np.abs(c)
        ans = min(S) + min(U)
    else:
        d = a - b[np.argmin(T)]
        V = np.abs(d)
        ans = min(T) + min(V)
    ls.append(ans)
for k in ls:
    print(k)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sun Feb 24 21:50:59 2019

@author: rubug
"""

import numpy as np
A, B, Q = map(int, input().split())
s = np.zeros(A)
t = np.zeros(B)
x = np.zeros(Q)
for i in range(A):
    s[i] = input()
for j in range(B):
    t[j] = input()
for k in range(Q):
    x[k] = input()
    D_E_shrine = 1.5*10**10
    D_W_shrine = 1.5*10**10
    for i in range(A):
        if (x[k] <= s[i]): ##神社が現在地以東
            if D_E_shrine >= (s[i] - x[k]):
                D_E_shrine = s[i] - x[k]
        elif (s[i] <= x[k]):
            if D_W_shrine >= (x[k] - s[i]):
                D_W_shrine = x[k] - s[i]
    D_E_temple = 1.5*10**10
    D_W_temple = 1.5*10**10
    for j in range(B):
        if (x[k] <= t[j]): ##寺が現在地以東
            if D_E_temple >= (t[j] - x[k]):
                D_E_temple = t[j] - x[k]
        elif (t[j] <= x[k]):
            if D_W_temple >= (x[k] - t[j]):
                D_W_temple = x[k] - t[j]
    D_E_E = max(D_E_shrine,D_E_temple)
    D_W_W = max(D_W_shrine,D_W_temple)
    D_E_W = min(D_E_shrine,D_W_temple) * 2 + max(D_E_shrine,D_W_temple)
    D_W_E = min(D_W_shrine,D_E_temple) * 2 + max(D_W_shrine,D_E_temple)
    print(min(D_E_E, D_W_W, D_E_W, D_W_E))

Submission status: Wrong Answer

Solution code:
from bisect import bisect_left
A, B, Q = map(int, input().split())
BIG = 10**10 + 1
S = [-BIG] + sorted([int(input()) for _ in range(A)]) + [BIG*2]
T = [-BIG] + sorted([int(input()) for _ in range(B)]) + [BIG*2]
for _ in range(Q):
  x = int(input())
  shrine_b = bisect_left(S, x)
  dsl = x-S[shrine_b-1]
  dsr = S[shrine_b]-x
  temple_b = bisect_left(T, x)
  dtl = x-T[temple_b-1]
  dtr = T[temple_b]-x

  print(min(
    dsl if dsl > dtl else dtl,
    dsr if dsr > dtr else dtr,
    dsl * 2 + dtr if dsl < dtr else dsl + dtr * 2,
    dsr * 2 + dtl if dsr < dtr else dsr + dtl * 2
  ))

Submission status: Wrong Answer

Solution code:
import sys
from sys import stdin
import numpy as np

def near(arr, num):
	index = np.abs(arr - num).argmin()
	return index

'''
A, B, Q = [int(i) for i in stdin.readline().split()]
s = np.asarray([int(stdin.readline()) for _ in range(A)])
t = np.asarray([int(stdin.readline()) for _ in range(B)])
x = [int(stdin.readline()) for _ in range(Q)]

'''
A, B, Q =[2, 3, 4]
s = [100,600]
t=[400,900,1000]
x=[150,2000,899,799]

st = np.hstack((s, t))
print(st, file=sys.stderr)
for i in range(Q):
	index = near(st, x[i])
	print(index, file=sys.stderr)
	if index<A:
		print(abs(x[i]-st[index]) + abs(st[index]-t[near(t, st[index])]))
	else:
		print(abs(x[i]-st[index]) + abs(st[index]-s[near(s, st[index])]))

Submission status: Wrong Answer

Solution code:
A,B,Q=list(map(int,input().split()))
s=[0]*(A+2)
t=[0]*(B+2)
X=[0]*Q
s[0] = -float('inf')
s[A+1] = float('inf')
t[0] = -float('inf')
t[B+1] = float('inf')
for i in range(1,A+1):
    s[i] = int(input())
for i in range(1,B+1):
    t[i] = int(input())
for i in range(Q):
    X[i] = int(input())

import bisect


def dec(x,s,t):
    s_ = s-x
    t_ = t-x
    as_ = abs(s_)
    at_ = abs(t_)
    if (s>0 and t<0) or (s<0 and t>0):
        return as_ + at_ + min(as_, at_)
    else:
        return max(as_,at_)


for x in X:
    s_i = bisect.bisect_left(s,x)
    t_i = bisect.bisect_left(t,x)
    s_l = s[s_i-1]
    s_r = s[s_i]
    t_l = t[t_i-1]
    t_r = t[t_i]
    ans = dec(x,s_l,t_l)
    ans_ = dec(x,s_l,t_r)
    if ans > ans_:
        ans = ans_
    ans_ = dec(x,s_r,t_l)
    if ans > ans_:
        ans = ans_
    ans_ = dec(x,s_r,t_r)
    if ans > ans_:
        ans = ans_
    print(ans)

Submission status: Wrong Answer

Solution code:
import bisect
a,b,c = map(int,input().split())

aa = []
bb = []
for i in range(a):
	aa.append(int(input()))
for i in range(b):
	bb.append(int(input()))
for i in range(c):
	cc = int(input())
	aaa = bisect.bisect_left(aa,cc)
	bbb = bisect.bisect_left(bb,cc)
	if aaa == 0 and bbb == 0:
		print(max(aa[0],bb[0]) - cc)
	elif aaa == a and bbb == b:
		print(cc - min(aa[a-1],bb[b-1]))
	elif aaa == 0:
		if bbb == b:
			print(min(abs(cc - aa[0]),abs(cc - bb[bbb-1])) + aa[0] - bb[bbb-1])
		else:
			aaaa = min(abs(bb[bbb] - aa[0])+min(abs(cc-aa[0]),abs(cc-bb[bbb])),abs(bb[bbb-1] - aa[0])+min(abs(cc-aa[a-1]),abs(cc-bb[bbb-1])))
			print(aaaa)
	elif bbb == 0:
		if aaa == a:
			print(min(abs(cc - aa[0]),abs(cc - bb[bbb-1])) + bb[0] - aa[aaa-1])
		else:
			bbbb = min(abs(bb[0] - aa[aaa-1])+min(abs(cc-aa[aaa-1]),abs(cc-bb[0])),abs(bb[0] - aa[aaa])+min(abs(cc-aa[aaa]),abs(cc-bb[0])))
			print(bbbb)
	elif aaa == a:
		aaaa = min(abs(bb[bbb] - aa[a-1])+min(abs(cc-aa[a-1]),abs(cc-bb[bbb])),abs(bb[bbb-1] - aa[a-1])+min(abs(cc-aa[a-1]),abs(cc-bb[bbb-1])))
		print(aaaa)
	elif bbb == b:
		bbbb = min(abs(bb[b-1] - aa[aaa-1])+min(abs(cc-aa[aaa-1]),abs(cc-bb[b-1])),abs(bb[b-1] - aa[aaa])+min(abs(cc-aa[aaa]),abs(cc-bb[b-1])))
		print(bbbb)
	else:
		aaaa = min(max(aa[aaa],bb[bbb])-cc,cc-min(aa[aaa-1],bb[bbb-1]),2*aa[aaa] - cc - bb[bbb-1],aa[aaa] + cc - 2 * bb[bbb-1])
		print(aaaa)

Submission status: Wrong Answer

Solution code:
import bisect
import sys
input = sys.stdin.readline

A, B, Q = map(int, input().split())
s = []
for i in range(A):
    s.append(int(input()))
t = []
for i in range(B):
    t.append(int(input()))
i = 0
j = 0
st = [10 ** 10] * A
ts = [10 ** 10] * B
while True:
    st[i] = min(st[i], abs(s[i] - t[j]))
    ts[j] = min(ts[j], abs(s[i] - t[j]))
    if i == A - 1 and j == B - 1:
        break
    if i == A - 1:
        j += 1
        continue
    if j == B - 1:
        i += 1
        continue
    if s[i] < t[j]:
        i += 1
    else:
        j += 1
        
for i in range(Q):
    x = int(input())
    j = bisect.bisect(s, x)
    ans = 2 * 10 ** 10
    if j > 0:
        ans = min(ans, abs(x-s[j-1])+st[j-1])
    if j < A:
        ans = min(ans, abs(x-s[j])+st[j])
    j = bisect.bisect(t, x)
    if j > 0:
        ans = min(ans, abs(x-t[j-1])+ts[j-1])
    if j < B:
        ans = min(ans, abs(x-t[j])+ts[j])
    print(ans)

Submission status: Wrong Answer

Solution code:
a, b, q = [int(i) for i in input().split(" ")]

s = [0] * a
t = [0] * b
x = [[0, 0]] * q

#a, b, q = [2, 3, 4]
#s = [100, 600]
#t = [400, 900, 1000]
#x = [[0, 150], [1, 2000], [2, 899], [3, 799]]

sd = 0
td = 0

for i in range(0, a):
    s[i] = int(input())
    
for i in range(0, b):
    t[i] = int(input())
    
for i in range(0, q):
    x[i] = [i, int(input())]

x = sorted(x, key = lambda k: k[1])
ans = [[0, 0]] * q

def sdd(i, x):
    if i < 0 or i >= a:
        return 10**10 + 1
    else:
        return abs(s[i] - x)
    
def tdd(i, x):
    if i < 0 or i >= b:
        return 10**10 + 1
    else:
        return abs(t[i] - x)
        
for j in range(0, q):
    bf = False
    for i in range(sd, a):
        if s[i] > x[j][1]:
            sd = i
            bf = True
            break
    if not bf:
        sd = a
        
    bf = False

    for i in range(td, b):
        if t[i] > x[j][1]:
            td = i
            bf = True
            break
    if not bf:
        td = b
    
    xj = x[j][1]
    a1 = min(max(sdd(sd - 1, xj), tdd(td - 1, xj)), max(sdd(sd, xj), tdd(td, xj)))
    a2 = min(2 * sdd(sd - 1, xj) + tdd(td, xj), 2 * tdd(td - 1, xj) + sdd(sd, xj), sdd(sd - 1, xj) + 2 * tdd(td, xj), tdd(td - 1, xj) + 2 * sdd(sd, xj))
    ans[j] = [x[j][0], min(a1, a2)]

ans = sorted(ans, key = lambda k: k[0])
for i in range(0, q):
    print(ans[i][1])

Submission status: Wrong Answer

Solution code:
num=list(map(int,input().split()))
s_list = [int(input()) for i in range(num[0])]
t_list = [int(input()) for i in range(num[1])]
x_list = [int(input()) for i in range(num[2])]

for x_ in x_list:
	min_s=min(list(map(lambda x: abs(x-x_), s_list)))
	min_t=min(list(map(lambda x: abs(x-x_), t_list)))
	if min_s>min_t:
		min_list=list(map(lambda x: abs(x-x_), t_list))
		min_index=min_list.index(min_t)
		print(min(list(map(lambda x: abs(x-t_list[min_index]), s_list)))+min_t)



	else:
		min_list=list(map(lambda x: abs(x-x_), s_list))
		min_index=min_list.index(min_s)
		print(min(list(map(lambda x: abs(x-s_list[min_index]), t_list)))+min_s)

Submission status: Wrong Answer

Solution code:
def calc(locate, start_point):
    if start_point <= min(locate):
        if locate[0] >= locate[shrine_no]: 
            print(locate[0] - start_point)
        else:
            print(locate[shrine_no] - start_point)
            
    elif start_point >= max(locate):
        if locate[shrine_no -1] >= locate[-1]:
            print(start_point - locate[-1])
        else:
            print(start_point - locate[shrine_no - 1])
            
    else:
        s_diff = start_point - locate[0]
        t_diff = start_point - locate[shrine_no]
        for i in range(1, shrine_no):
            if abs(s_diff) > abs(start_point - locate[i]):
                s_diff = start_point - locate[i]
                
        for i in range(shrine_no+1, shrine_no + temple_no):
            if abs(t_diff) > abs(start_point - locate[i]):
                t_diff = start_point - locate[i]
                
        if s_diff * t_diff < 0:
            s_diff_list = []
            t_diff_list = []
            
            for i in range(shrine_no):
                s_diff_list.append(start_point - locate[i])
            sort_s_diff = sorted(s_diff_list)
                
            for i in range(shrine_no, shrine_no + temple_no):
                t_diff_list.append(start_point - locate[i])
            sort_t_diff = sorted(t_diff_list)

            dist = 10000
            for i in range(len(s_diff_list)):
                for j in range(len(t_diff_list)):
                    if (abs(s_diff_list[i]-t_diff_list[j]) + min(abs(s_diff_list[i]), abs(t_diff_list[j]))) < dist:
                        dist = abs(s_diff_list[i]-t_diff_list[j]) + min(abs(s_diff_list[i]), abs(t_diff_list[j]))
            print(dist)
            
        elif s_diff * t_diff > 0:
            print(max(abs(s_diff), abs(t_diff)))
        else:
            print(abs(s_diff+t_diff))

shrine_no, temple_no, q = map(int, input().split())
locate = []
start = []

for i in range(shrine_no+temple_no+q):
    tmp = int(input())
    if i < shrine_no+temple_no:
        locate.append(tmp)
    else:
        start.append(tmp)

for i in range(len(start)):
    calc(locate, start[i])

Submission status: Wrong Answer

Solution code:
import numpy as np
A, B, Q = map(int,input().split())
s_list = [int(input()) for i in range(A)]
t_list = [int(input()) for i in range(B)]
x_list = [int(input()) for i in range(Q)]


def get_dist_to_first_cp(x,li):
    dist1 = np.array(li) -x 
    abs_dist1 = abs(dist1)
    dist_to_first_cp = min(abs_dist1)
    first_cp_num = np.argmin(abs_dist1)
    first_cp_pos = x + dist1[first_cp_num]
    return dist_to_first_cp, first_cp_pos
    


u_list = s_list + t_list
for i, x in enumerate(x_list):
    # shrine first
    dist_to_first_cp, first_cp_pos = get_dist_to_first_cp(x,s_list)
    dist2 = abs(np.array(t_list) - dist_to_first_cp)
    dist_to_sec_cp = min(dist2)
    ans_s = dist_to_first_cp + dist_to_sec_cp
    # temple first
    dist_to_first_cp, first_cp_pos = get_dist_to_first_cp(x,t_list)
    dist2 = abs(np.array(s_list) - dist_to_first_cp)
    dist_to_sec_cp = min(dist2)

    ans_t = dist_to_first_cp + dist_to_sec_cp

    print(min(ans_s,ans_t))

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sun Feb 24 21:50:59 2019

@author: rubug
"""

import numpy as np
A, B, Q = map(int, input().split())
s = np.zeros(A)
t = np.zeros(B)
x = np.zeros(Q)
for i in range(A):
    s[i] = input()
for j in range(B):
    t[j] = input()
for k in range(Q):
    x[k] = input()
    D_E_shrine = 10**11
    D_W_shrine = 10**11
    for i in range(A):
        if (x[k] <= s[i]): ##神社が現在地以東
            if D_E_shrine >= (s[i] - x[k]):
                D_E_shrine = s[i] - x[k]
        elif (s[i] <= x[k]):
            if D_W_shrine >= (x[k] - s[i]):
                D_W_shrine = x[k] - s[i]
    D_E_temple = 10**11
    D_W_temple = 10**11
    for j in range(B):
        if (x[k] <= t[j]): ##寺が現在地以東
            if D_E_temple >= (t[j] - x[k]):
                D_E_temple = t[j] - x[k]
        elif (t[j] <= x[k]):
            if D_W_temple >= (x[k] - t[j]):
                D_W_temple = x[k] - t[j]
    D_E_E = max(D_E_shrine,D_E_temple)
    D_W_W = max(D_W_shrine,D_W_temple)
    D_E_W = min(D_E_shrine,D_W_temple) * 2 + max(D_E_shrine,D_W_temple)
    D_W_E = min(D_W_shrine,D_E_temple) * 2 + max(D_W_shrine,D_E_temple)
    print(min(D_E_E, D_W_W, D_E_W, D_W_E))

Submission status: Wrong Answer

Solution code:
def nibuntansaku(l,x):
    n = len(l)
    def hantei(med):
        if med == -1:
            return False
        elif med == n:
            return True
        elif l[med] > x:
            return True
        else:
            return False
    left = -1
    right =n
    while right-left >1:
        med = (left+right)//2
        if hantei(med):
            right = med
        else:
            left = med
    ret =[0,0]
    if left == -1:
        ret[0] = 10**11
        ret[1] = l[right] -x
    elif right == n:
        ret[0] = x -l[left]
        ret[1] = 10**11
    else:
        ret[0] = x - l[left]
        ret[1] = l[right] -x
    return ret


A,B,Q =[int(i)for i in input().split()]
S =[int(input())for _ in range(A)]
T =[int(input())for _ in range(B)]

for _ in range(Q):
    x = int(input())
    slef,sri = nibuntansaku(S,x)
    tlef,tri = nibuntansaku(T,x)
    lmax,lmin = max(slef,tlef),min(slef,tlef)
    rmax,rmin = max(sri,tri),min(sri,tri)
    ans = min(lmax,rmax,2*lmin+rmin,2*rmin+lmin)
    print(ans)

Submission status: Wrong Answer

Solution code:
import bisect
a,b,c = map(int,input().split())

aa = []
bb = []
for i in range(a):
	aa.append(int(input()))
for i in range(b):
	bb.append(int(input()))
for i in range(c):
	cc = int(input())
	aaa = bisect.bisect_left(aa,cc)
	bbb = bisect.bisect_left(bb,cc)
	if aaa == 0 and bbb == 0:
		print(max(aa[0],bb[0]) - cc)
	elif aaa == a and bbb == b:
		print(cc - min(aa[a-1],bb[b-1]))
	elif aaa == 0:
		if bbb == b:
			print(min(abs(cc - aa[0]),abs(cc - bb[bbb-1])) + aa[0] - bb[bbb-1])
		else:
			aaaa = min(abs(bb[bbb] - aa[0])+min(abs(cc-aa[0]),abs(cc-bb[bbb])),abs(bb[bbb-1] - aa[0])+min(abs(cc-aa[a-1]),abs(cc-bb[bbb-1])))
			print(aaaa)
	elif bbb == 0:
		if aaa == a:
			print(min(abs(cc - aa[0]),abs(cc - bb[bbb-1])) + bb[0] - aa[aaa-1])
		else:
			bbbb = min(abs(bb[0] - aa[aaa-1])+min(abs(cc-aa[aaa-1]),abs(cc-bb[0])),abs(bb[0] - aa[aaa])+min(abs(cc-aa[aaa]),abs(cc-bb[0])))
			print(bbbb)
	elif aaa == a:
		aaaa = min(abs(bb[bbb] - aa[a-1])+min(abs(cc-aa[a-1]),abs(cc-bb[bbb])),abs(bb[bbb-1] - aa[a-1])+min(abs(cc-aa[a-1]),abs(cc-bb[bbb-1])))
		print(aaaa)
	elif bbb == b:
		bbbb = min(abs(bb[b-1] - aa[aaa-1])+min(abs(cc-aa[aaa-1]),abs(cc-bb[b-1])),abs(bb[b-1] - aa[aaa])+min(abs(cc-aa[aaa]),abs(cc-bb[b-1])))
		print(abs(cc - bb[b-1]) + bbbb)
	else:
		aaaa = min(max(aa[aaa],bb[bbb])-cc,cc-min(aa[aaa-1],bb[bbb-1]),2*aa[aaa] - cc - bb[bbb-1],aa[aaa] + cc - 2 * bb[bbb-1])
		print(aaaa)

Submission status: Wrong Answer

Solution code:
from bisect import bisect_left
a, b, q = [int(i) for i in input().split()]
ss = [int(input()) for i in range(a)]
ts = [int(input()) for i in range(b)]
xs = [int(input()) for i in range(q)]
# print(ss, ts, xs)
for x in xs:
    idx_s = bisect_left(ss, x)
    idx_t = bisect_left(ts, x)
    # print(idx_s, idx_t)
    if idx_s == len(ss) and idx_t == len(ts):
        ans1 = 10e10
    elif idx_t == len(ts):
        ans1 = (ss[idx_s] - x) + min([abs(t - ss[idx_s]) for t in ts])
    elif idx_s == len(ss):
        ans1 = (ts[idx_t] - x) + min([abs(s - ts[idx_t]) for s in ss])
    else:
        if ss[idx_s] - x < ts[idx_t] - x:
            ans1 = (ss[idx_s] - x) + min([abs(t - ss[idx_s]) for t in ts])
        else:
            ans1 = (ts[idx_t] - x) + min([abs(s - ts[idx_t]) for s in ss])

    if idx_s == 0 and idx_t == 0:
        ans2 = 10e10
    elif idx_t == 0:
        ans2 = (x - ss[idx_s-1]) + min([abs(t - ss[idx_s-1]) for t in ts])
    elif idx_s == 0:
        ans2 = (x - ts[idx_t-1]) + min([abs(s - ts[idx_t]-1) for s in ss])
    else:
        if x - ss[idx_s-1] < x - ts[idx_t-1]:
            ans2 = (x - ss[idx_s-1]) + min([abs(t - ss[idx_s-1]) for t in ts])
        else:
            ans2 = (x - ts[idx_t-1]) + min([abs(s - ts[idx_t-1]) for s in ss])

    # print(ans1)
    # print(ans1, ans2)
    print(min(ans1, ans2))

Submission status: Wrong Answer

Solution code:
a, b, q = map(int, input().split())
s = [int(input()) for _ in range(a)]
t = [int(input()) for _ in range(b)]

s.sort()
t.sort()

def near(x, buildings):
    left, right = 0, len(buildings) - 1

    while right - left > 1:
        center = (left + right) // 2
        value = buildings[center]
        if x < value:
            right = center
        elif x > value:
            left = center
        else:
            return center
    return left if abs(x - buildings[left]) < abs(x - buildings[right]) else right


def cost(a, b, x):
    if a <= x and b <= x:
        return x - min(a, b)
    elif a >= x and b >= x:
        return max(a, b) - x
    else:
        abs_a = abs(x - a)
        abs_b = abs(x - b)
        return min(abs_a, abs_b) * 2 + max(abs_a, abs_b)

for _ in range(q):
    x = int(input())

    si = near(x, s)
    ti = near(s[si], t)
    cost_ab = cost(s[si], t[ti], x)

    ti = near(x, t)
    si = near(t[ti], s)
    cost_ba = cost(s[si], t[ti], x)
    print(min(cost_ab, cost_ba))

Submission status: Wrong Answer

Solution code:
A, B, Q = map(int, input().split())
s_list = [int(input()) for i in range(A)]
t_list = [int(input()) for i in range(B)]
x_list = [int(input()) for _ in range(Q)]

s_list.sort()
t_list.sort()
import bisect

ans = []
for x in x_list:
    tmp = float("inf")
    s_i = bisect.bisect_left(s_list, x)
    t_i = bisect.bisect_left(t_list, x)

    if s_i == A and t_i == B:
        tmp = abs(x - min(s_list[-1], t_list[-1]))
        ans.append(tmp)
        continue
    if s_i == A:
        if t_i != 0:
            tmp = abs(s_list[-1] - x)
            if s_list[-1] <= t_list[t_i] <= x:
                ans.append(tmp)
                continue
            else:
                tmp += min(abs(t_list[t_i] - x) * 2, abs(t_list[t_i - 1] - s_list[-1]))
        else:
            tmp = abs(min(abs(s_list[s_i - 1] - x), abs(t_list[0] - x)) * 2 + max(abs(s_list[s_i - 1] - x),
                                                                                  abs(t_list[0] - x)))
        ans.append(tmp)
        continue
    if t_i == B:
        if s_i != 0:
            tmp = abs(t_list[-1] - x)
            if t_list[-1] <= s_list[s_i] <= x:
                ans.append(tmp)
                continue
            else:
                tmp += min(abs(s_list[s_i] - x) * 2, abs(s_list[s_i - 1] - t_list[-1]))
        else:
            tmp = abs(min(abs(t_list[t_i - 1] - x), abs(s_list[0] - x)) * 2 + max(abs(t_list[t_i - 1] - x),
                                                                                  abs(s_list[0] - x)))
        ans.append(tmp)
        continue

    # S1, T1
    if s_i != 0 and t_i != 0:
        tmp = min(tmp, abs(min(s_list[s_i - 1], t_list[t_i - 1]) - x))
    elif s_i == 0 and t_i == 0:
        tmp = min(tmp, abs(x - max(s_list[0], t_list[0])))
    elif s_i == 0 and t_i != 0:
        tmp = min(tmp, abs(min(abs(t_list[t_i] - x), abs(s_list[s_i] - x)) * 2 + max(abs(t_list[t_i] - x),
                                                                                         abs(s_list[s_i] - x))))
    else:
        tmp = min(tmp, abs(min(abs(t_list[t_i] - x), abs(s_list[s_i] - x)) * 2 + max(abs(t_list[t_i] - x),
                                                                                         abs(s_list[s_i] - x))))

    # S2, T2
    tmp = min(tmp, abs(max(s_list[s_i], t_list[t_i]) - x))

    # S1, T2
    if s_i != 0:
        tmp = min(tmp, abs(min(abs(t_list[t_i] - x), abs(s_list[s_i - 1] - x)) * 2 + max(abs(t_list[t_i] - x),
                                                                                         abs(s_list[s_i - 1] - x))))
    else:
        tmp = min(tmp, abs(max(t_list[t_i], s_list[0]) - x))
    # S2, T1
    if t_i != 0:
        tmp = min(tmp, abs(min(abs(s_list[s_i] - x), abs(t_list[t_i - 1] - x)) * 2 + max(abs(s_list[s_i] - x),
                                                                                         abs(t_list[t_i - 1] - x))))
    else:
        tmp = min(tmp, abs(max(s_list[s_i], t_list[0]) - x))
    ans.append(tmp)
for a in ans:
    print(a)

Submission status: Wrong Answer

Solution code:
import numpy as np

hint = list(map(int, input().split()))
s = [int(input()) for i in range(sum(hint))]
#print(s)

a = np.asarray(s[:hint[0]])
b = np.asarray(s[hint[0]:-hint[2]])
sum = 0
for t in s[-hint[2]:]:
    a_dis = np.abs(a-t).min()
    b_dis = np.abs(b-t).min()
    print(np.min([np.abs(b_dis-t) + np.min(np.abs(a-b_dis)), np.abs(b_dis-t) + np.min(np.abs(b-a_dis))]))

Submission status: Wrong Answer

Solution code:
A,B,Q=map(int,input().split())
s=[]
t=[]
for i in range(A):
  s.append(int(input()))
for i in range(B):
  t.append(int(input()))
for i in range(Q):
  x=int(input())
  s1=s[:]
  s1.append(x)
  j=s1.index(x)
  if j==0:
    kouhoA=[s[j]]
  elif j==A:
    kouhoA=[s[j-1]]
  else:
    kouhoA=[s[j],s[j-1]]
  t1=t[:]
  t1.append(x)
  t1.sort()
  k=t1.index(x)
  if k==0:
    kouhoB=[t[k]]
  elif k==B:
    kouhoB=[t[k-1]]
  else:
    kouhoB=[t[k],t[k-1]]
  if len(kouhoA)==1:
    if len(kouhoB)==1:
      ans=min([abs(x-kouhoA[0])+abs(kouhoA[0]-kouhoB[0]),abs(x-kouhoB[0])+abs(kouhoB[0]-kouhoA[0])])
    else:
      ans=min([abs(x-kouhoA[0])+abs(kouhoA[0]-kouhoB[0]),abs(x-kouhoB[0])+abs(kouhoB[0]-kouhoA[0]),abs(x-kouhoA[0])+abs(kouhoA[0]-kouhoB[1]),abs(x-kouhoB[1])+abs(kouhoB[1]-kouhoA[0])])
  else:
    if len(kouhoB)==1:
      ans=min([abs(x-kouhoA[0])+abs(kouhoA[0]-kouhoB[0]),abs(x-kouhoB[0])+abs(kouhoB[0]-kouhoA[0]),abs(x-kouhoA[1])+abs(kouhoA[1]-kouhoB[0]),abs(x-kouhoB[0])+abs(kouhoB[0]-kouhoA[1])])
    else:
      ans=min([abs(x-kouhoA[0])+abs(kouhoA[0]-kouhoB[0]),abs(x-kouhoB[0])+abs(kouhoB[0]-kouhoA[0]),abs(x-kouhoA[1])+abs(kouhoA[1]-kouhoB[1]),abs(x-kouhoB[1])+abs(kouhoB[1]-kouhoA[1]),abs(x-kouhoA[1])+abs(kouhoA[1]-kouhoB[0]),abs(x-kouhoB[0])+abs(kouhoB[0]-kouhoA[1]),abs(x-kouhoA[0])+abs(kouhoA[0]-kouhoB[1]),abs(x-kouhoB[1])+abs(kouhoB[1]-kouhoA[0])])
  print(ans)

Submission status: Wrong Answer

Solution code:
import bisect

A, B, Q = map(int, input().split())
s = []
for i in range(A):
    s.append(int(input()))
t = []
for i in range(B):
    t.append(int(input()))
i = 0
j = 0
st = [10 ** 10] * A
ts = [10 ** 10] * B
while True:
    st[i] = min(st[i], abs(s[i] - t[j]))
    ts[j] = min(ts[j], abs(s[i] - t[j]))
    if i == A - 1 and j == B - 1:
        break
    if i == A - 1:
        j += 1
        continue
    if j == B - 1:
        i += 1
        continue
    if s[i] < t[j]:
        i += 1
    else:
        j += 1
        
for i in range(Q):
    x = int(input())
    j = bisect.bisect(s, x)
    ans = 2 * 10 ** 10
    if j > 0:
        ans = min(ans, abs(x-s[j-1])+st[j-1])
    if j < A:
        ans = min(ans, abs(x-s[j])+st[j])
    j = bisect.bisect(t, x)
    if j > 0:
        ans = min(ans, abs(x-t[j-1])+ts[j-1])
    if j < B:
        ans = min(ans, abs(x-t[j])+ts[j])
    print(ans)

Submission status: Wrong Answer

Solution code:
A,B,Q = map(int,input().split())
As = [int(input()) for a in range(A)]
Bs = [int(input()) for b in range(B)]
Xs = [int(input()) for q in range(Q)]
X_A = [[0 for a in range(len(As))] for x in range(len(Xs))]
X_B = [[0 for a in range(len(Bs))] for x in range(len(Xs))]
A_B = [[0 for a in range(len(Bs))] for x in range(len(As))]
B_A = [[0 for a in range(len(As))] for x in range(len(Bs))]
ans = []
for x in range(len(Xs)):
    for a in range(len(As)):
        X_A[x][a] = abs(Xs[x]-As[a])
    for b in range(len(Bs)):
        X_B[x][b] = abs(Xs[x]-Bs[b]) 
for a in range(len(As)):
    for b in range(len(Bs)):
        A_B[a][b] = abs(As[a]-Bs[b])
for b in range(len(Bs)):
    for a in range(len(As)):
        B_A[b][a] = abs(Bs[b]-As[a])
for (a,b) in zip(X_A,X_B):
    ascore = 0
    bscore = 0
    akouho = min(a)
    bkouho = min(b)
    ascore+=akouho
    ascore+=min(A_B[a.index(akouho)])
    bscore+=bkouho
    bscore+=min(B_A[b.index(bkouho)])
    ans.append(min(ascore,bscore))
print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np

a,b,q=map(int,input().split())

ST=[]
for i in range(a):
    k=int(input())
    ST.append([k,0,0])
for i in range(b):
    k=int(input())
    ST.append([k,1,0])

ST.sort()

le=[-1,-1]
ri=[1,1]

for i in range(a+b):
    ty=ST[i][1]
    pa=(ty+1)%2
    
    moyori=np.inf

    if le[pa]>=0:
        moyori=min(moyori,abs(ST[i][0]-ST[le[pa]][0]))
    if ri[pa]<=i :
        while ri[pa]<=a+b-1 and ST[ri[pa]][1]==ty :
            ri[pa]+=1
    if ri[pa]<=a+b-1:
        moyori=min(moyori,abs(ST[i][0]-ST[ri[pa]][0]))
    ST[i][2]=moyori
    le[ty]=i

#print(ST)

for i in range(q):
    k=int(input())
    ST.append([k,2,0,i])

ST.sort()

ANS=[]
le=[-1,-1]
ri=[1,1]
for i in range(a+b+q):
    if ST[i][1]==0 :
        le[0]=i
    elif ST[i][1]==1 :
        le[1]=i
    elif ST[i][1]==2 :
        moyori=np.inf
        for typ in range(2):
            
            if le[typ]>=0:
                moyori=min(moyori,abs(ST[i][0]-ST[le[typ]][0])+ST[le[typ]][2])
                #print("L"+str(le[typ]))
                #print(moyori)
            if ri[typ]<=i :
                ri[typ]=i+1
                while ri[typ]<=a+b+q-1 and (ST[ri[typ]][1]==(typ+1)%2 or ST[ri[typ]][1]==2):
                    ri[typ]+=1
            if ri[typ]<=a+b+q-1 :
                moyori=min(moyori,abs(ST[i][0]-ST[ri[typ]][0])+ST[ri[typ]][2])
                #print("R"+str(ri[typ]))
                #print(moyori)
        ST[i][2]=moyori
        ANS.append([ST[i][3],moyori])

ANS.sort()

#print(ST)

for i in range(q):
    print(ANS[i][1])