input
stringlengths
20
127k
target
stringlengths
20
119k
problem_id
stringlengths
6
6
from collections import defaultdict import sys input = sys.stdin.readline N = int(eval(input())) a = list(map(int, input().split())) pow2 = [1] for i in range(31): pow2.append(pow2[-1] * 2) a = sorted(a, reverse=True) d = defaultdict(int) for i in a: d[i] += 1 num = 0 for i in range(N): if d[a[i]] == 0: continue for k in range(30, -1, -1): if pow2[k] - a[i] != a[i]: if d[pow2[k] - a[i]] > 0 and d[a[i]] > 0: p = min(d[pow2[k] - a[i]], d[a[i]]) num += p d[pow2[k] - a[i]] -= p d[a[i]] -= p break else: if d[a[i]] >= 2: num += d[a[i]] // 2 d[a[i]] %= 2 break print(num)
from collections import defaultdict import sys input = sys.stdin.readline N = int(eval(input())) a = list(map(int, input().split())) pow2 = [1] for i in range(31): pow2.append(pow2[-1] * 2) a = sorted(a, reverse=True) d = defaultdict(int) for i in a: d[i] += 1 num = 0 for i in range(N): if d[a[i]] == 0: continue for k in range(30, -1, -1): inv = pow2[k] - a[i] if inv < 0 or inv > a[i]: continue if inv != a[i]: if d[inv] > 0 and d[a[i]] > 0: p = min(d[inv], d[a[i]]) num += p d[inv] -= p d[a[i]] -= p break else: if d[a[i]] >= 2: num += d[a[i]] // 2 d[a[i]] %= 2 break print(num)
p03201
from collections import Counter def larger_nibeki(i): return 2 ** len(format(i, "b")) n = int(eval(input())) a = list(map(int, input().split())) a.sort(reverse=True) ans = 0 c = Counter(a) #print(larger_nibeki(n)) for i in range(n): if c[a[i]] == 0: continue else: c[a[i]] -= 1 if c[larger_nibeki(a[i]) - a[i]] > 0: ans += 1 c[larger_nibeki(a[i]) - a[i]] -= 1 print(ans)
from collections import Counter def larger_nibeki(i): return 2 ** len(format(i, "b")) n = int(eval(input())) a = list(map(int, input().split())) a.sort(reverse=True) ans = 0 c = Counter(a) for i in range(n): if c[a[i]] == 0: continue else: c[a[i]] -= 1 if c[larger_nibeki(a[i]) - a[i]] > 0: ans += 1 c[larger_nibeki(a[i]) - a[i]] -= 1 print(ans)
p03201
from bisect import bisect N = int(eval(input())) *A, = list(map(int, input().split())) B = {} for a in A: B[a] = B.get(a, 0) + 1 R = sorted(B.items()) C = []; D = [] for k, v in sorted(B.items()): C.append(k) D.append(v) M = len(C) j = 0 ans = 0 for i in range(M-1, -1, -1): if D[i] == 0: continue a = C[i] b = 2**(a).bit_length() j = bisect(C, b-a-1) if j < M and C[j] == b-a: if i == j: ans += D[i]//2 D[i] %= 2 else: c = min(D[i], D[j]) ans += c D[i] -= c; D[j] -= c print(ans)
from bisect import bisect N = int(eval(input())) *A, = list(map(int, input().split())) B = {} for a in A: B[a] = B.get(a, 0) + 1 R = sorted(B.items()) C = []; D = [] for k, v in sorted(B.items()): C.append(k) D.append(v) M = len(C) j = 0 ans = 0 for i in range(M-1, -1, -1): if D[i] == 0: continue a = C[i] b = 2**a.bit_length() j = bisect(C, b-a-1) if j < M and C[j] == b-a: if i == j: c = D[i]//2 else: c = min(D[i], D[j]) ans += c D[i] -= c; D[j] -= c print(ans)
p03201
def main(): from collections import Counter import sys input = sys.stdin.buffer.readline _ = int(eval(input())) A = [int(i) for i in input().split()] ans = 0 c = Counter(A) B = sorted(list(c.keys()), reverse=True) for b in B: if c[b] == 0: continue a = (2 ** b.bit_length()) - b if a == b: cur = c[b]//2 else: if a in list(c.keys()): cur = min(c[a], c[b]) else: cur = 0 ans += cur # c[b] -= cur c[a] -= cur print(ans) if __name__ == '__main__': main()
def main(): from collections import Counter import sys input = sys.stdin.buffer.readline _ = int(eval(input())) A = [int(i) for i in input().split()] ans = 0 c = Counter(A) B = sorted(list(c.keys()), reverse=True) for b in B: if c[b] == 0: continue a = (2 ** b.bit_length()) - b if a == b: cur = c[b]//2 else: if a in c: cur = min(c[a], c[b]) else: cur = 0 ans += cur # c[b] -= cur c[a] -= cur print(ans) if __name__ == '__main__': main()
p03201
import bisect from collections import defaultdict n = int(eval(input())) a = list(map(int, input().split())) a.sort() memo = defaultdict(int) for i in range(n): memo[a[i]] += 1 count = 0 for t in reversed(list(range(1, 31))): for i in reversed(list(range(n))): if memo[a[i]] == 0: continue front = bisect.bisect_left(a, (1 << t) - a[i]) back = bisect.bisect_right(a, (1 << t) - a[i]) if front == back: continue if a[front] == a[i] and memo[a[i]] <= 1: continue if memo[a[front]] >= 1: memo[a[front]] -= 1 memo[a[i]] -= 1 count += 1 print(count)
from collections import defaultdict n = int(eval(input())) a = list(map(int, input().split())) a.sort() memo = defaultdict(int) for i in range(n): memo[a[i]] += 1 count = 0 for i in reversed(list(range(n))): if memo[a[i]] == 0: continue if (memo[(1 << (len(bin(a[i])) - 2)) - a[i]] > 0 and (1 << (len(bin(a[i])) - 2)) - a[i] != a[i]) or memo[(1 << (len(bin(a[i])) - 2)) - a[i]] > 1: memo[a[i]] -= 1 memo[(1 << (len(bin(a[i])) - 2)) - a[i]] -= 1 count += 1 print(count)
p03201
#!/usr/bin/env python3 import sys, os try: from typing import Any, Union, List, Tuple, Dict except ImportError: pass sys.setrecursionlimit(10**6) def debug(*args): True if os.environ.get("USER")=="shun" else None def exit(): sys.exit(0) n = int(eval(input())) a = list(map(int, input().split())) a.sort() max_a = a[-1] m = 0 while True: if 2**m > max_a: break m += 1 used = [False] * n def bisect(val, start, end): debug(used) if start >= end: return None elif start + 1 == end: if a[start] == val: if used[start]: return None used[start] = True return start else: return None mid = (start + end) // 2 if a[mid] == val: if used[mid]: res = bisect(val, start, mid) return res else: res = bisect(val, mid+1, end) if res is not None: return res used[mid] = True return mid # if used[mid]: # res = bisect(val, mid+1, end) # if res is not None: # return res # res = bisect(val, start, mid) # return res # else: # used[mid] = True # debug("fuga", used) # return mid elif a[mid] > val: return bisect(val, start, mid) else: return bisect(val, mid, end) # for i in reversed(range(end)): # if (not used[i]) and (a[i] == val): # used[i] = True # return i return None def _find(b, end, e): # debug(b, end, e) val = 2**e - b if val <= 0: return None return bisect(val, 0, end) # for i in reversed(range(end)): # if (not used[i]) and (a[i] + b == 2**e): # used[i] = True # return i # return None def find(b, end): for e in reversed(list(range(m+1))): res = _find(b, end, e) if res is not None: return True return False ans = 0 for j in reversed(list(range(n))): if used[j]: continue b = a[j] used[j] = True res = find(b, j) if res: ans += 1 print(ans)
#!/usr/bin/env python3 import sys, os try: from typing import Any, Union, List, Tuple, Dict except ImportError: pass sys.setrecursionlimit(10**6) def debug(*args): pass # if os.environ.get("USER")=="shun" else None def exit(): sys.exit(0) n = int(eval(input())) a = list(map(int, input().split())) a.sort() max_a = a[-1] m = 0 while True: if 2**m > max_a: break m += 1 used = [False] * n def bisect(val, start, end): debug(used) if start >= end: return None elif start + 1 == end: if a[start] == val: if used[start]: return None used[start] = True return start else: return None mid = (start + end) // 2 if a[mid] == val: if used[mid]: res = bisect(val, start, mid) return res else: res = bisect(val, mid+1, end) if res is not None: return res used[mid] = True return mid # if used[mid]: # res = bisect(val, mid+1, end) # if res is not None: # return res # res = bisect(val, start, mid) # return res # else: # used[mid] = True # debug("fuga", used) # return mid elif a[mid] > val: return bisect(val, start, mid) else: return bisect(val, mid, end) # for i in reversed(range(end)): # if (not used[i]) and (a[i] == val): # used[i] = True # return i return None def _find(b, end, e): # debug(b, end, e) val = 2**e - b if val <= 0: return None return bisect(val, 0, end) # for i in reversed(range(end)): # if (not used[i]) and (a[i] + b == 2**e): # used[i] = True # return i # return None def find(b, end): for e in reversed(list(range(m+1))): res = _find(b, end, e) if res is not None: return True return False ans = 0 for j in reversed(list(range(n))): if used[j]: continue b = a[j] used[j] = True res = find(b, j) if res: ans += 1 print(ans)
p03201
n = int(eval(input())) A = list(map(int,input().split())) A.append(-float("inf")) n = len(A) A.sort() s = 2 while s <= A[-1]+A[-2]: s *= 2 s //= 2 U = [0] * n ans = 0 while s > 0: ll = 0 rr = n-1 while rr >= 1 and rr-ll >= 2: # print("rr",rr,"ll",ll) t = s - A[rr] if t>s: break # print("s",s,"t",t) l = ll r = rr-1 m = (r+l)//2 while r-l > 1: # print("r",r,"l",l,"m",m) if A[m] >= t: r = m else: l = m m = (r+l)//2 if A[l+1] == t: ans += 1 U[l+1] = 1 U[rr] = 1 # print(ans) A[l+1] = -float("inf") ll = l rr -= 1 # print(A) s //= 2 B = [] for i in range(len(A)): if U[i] == 0: B.append(A[i]) A = B n = len(A) U = [0] * n print(ans)
n = int(eval(input())) A = list(map(int,input().split())) A.append(-float("inf")) n = len(A) A.sort() s = 2 while s <= A[-1]+A[-2]: s *= 2 s //= 2 U = [0] * n ans = 0 while s > 0: ll = 0 rr = n-1 while rr >= 1 and s <= A[rr]: rr -= 1 while rr >= 1 and rr-ll >= 2: # print("rr",rr,"ll",ll) t = s - A[rr] # print("s",s,"t",t) l = ll r = rr-1 m = (r+l)//2 while r-l > 1: # print("r",r,"l",l,"m",m) if A[m] >= t: r = m else: l = m m = (r+l)//2 if A[l+1] == t: ans += 1 U[l+1] = 1 U[rr] = 1 # print(ans) A[l+1] = -float("inf") ll = l rr -= 1 # print(A) s //= 2 B = [] for i in range(len(A)): if U[i] == 0: B.append(A[i]) A = B n = len(A) U = [0] * n print(ans)
p03201
import collections N = eval(input()) A = list(map(int, input().split())) C = collections.Counter(A) K = list(C.keys()) K.sort(reverse=1) res = 0 for i in K: for j in range(60): p = 2 ** j if i > p: continue elif i == p: res += C[i] // 2 C[i] = C[i] // 2 * 2 else: num = min(C[i], C[p - i]) res += num C[i] -= num C[p - i] -= num print(res)
import collections N = eval(input()) A = list(map(int, input().split())) C = collections.Counter(A) K = list(C.keys()) K.sort(reverse=1) res = 0 for i in K: for j in range(60): p = 2 ** j if i <= p: break if i == p: res += C[i] // 2 C[i] = C[i] // 2 * 2 else: num = min(C[i], C[p - i]) res += num C[i] -= num C[p - i] -= num print(res)
p03201
from bisect import bisect_right n = int(eval(input())) d = {} amax = 0 for a in map(int, input().split()): if a in d: d[a] += 1 else: d[a] = 1 amax = max(d.keys()) t = 1 valids = [] while t < 2 * amax: t *= 2 valids.append(t) result = 0 while True: prev = result keys = list(sorted(list(d.keys()), reverse = True)) for x in keys: if x not in d: continue t = valids[bisect_right(valids, x)] - x while True: if t not in d: break if x == t and d[x] == 1: break result += 1 if d[x] == 1: del d[x] else: d[x] -= 1 if d[t] == 1: del d[t] else: d[t] -= 1 if x not in d: break if prev == result: break print(result)
from bisect import bisect_right n = int(eval(input())) d = {} for a in map(int, input().split()): if a in d: d[a] += 1 else: d[a] = 1 amax = max(d.keys()) t = 1 valids = [] while t < 2 * amax: t *= 2 valids.append(t) result = 0 keys = list(sorted(list(d.keys()), reverse = True)) for a in keys: if a not in d: continue t = valids[bisect_right(valids, a)] - a while True: if t not in d: break if a == t and d[a] == 1: break result += 1 if d[a] == 1: del d[a] else: d[a] -= 1 if d[t] == 1: del d[t] else: d[t] -= 1 if a not in d: break print(result)
p03201
from bisect import bisect_right n = int(eval(input())) d = {} for a in map(int, input().split()): if a in d: d[a] += 1 else: d[a] = 1 alist = list(sorted(list(d.keys()), reverse = True)) amax = alist[0] t = 1 power_of_two = [] while t < 2 * amax: t *= 2 power_of_two.append(t) result = 0 for a in alist: if d[a] == 0: continue t = power_of_two[bisect_right(power_of_two, a)] - a while True: if t not in d or d[t] == 0: break if a != t: i = min(d[a], d[t]) else: if d[a] == 1: break i = d[a] // 2 result += i d[a] -= i d[t] -= i if d[a] == 0: break print(result)
from bisect import bisect_right n = int(eval(input())) d = {} for a in map(int, input().split()): if a in d: d[a] += 1 else: d[a] = 1 alist = list(sorted(list(d.keys()), reverse = True)) amax = alist[0] t = 1 power_of_two = [] while t < 2 * amax: t *= 2 power_of_two.append(t) result = 0 for a in alist: if d[a] == 0: continue t = power_of_two[bisect_right(power_of_two, a)] - a if t not in d or d[t] == 0: continue if a != t: i = min(d[a], d[t]) else: if d[a] == 1: continue i = d[a] // 2 result += i d[a] -= i d[t] -= i print(result)
p03201
from bisect import bisect_right n = int(eval(input())) d = {} for a in map(int, input().split()): if a in d: d[a] += 1 else: d[a] = 1 alist = list(sorted(list(d.keys()), reverse = True)) amax = alist[0] t = 1 power_of_two = [] while t < 2 * amax: t *= 2 power_of_two.append(t) result = 0 for a in alist: if d[a] == 0: continue t = power_of_two[bisect_right(power_of_two, a)] - a if t not in d or d[t] == 0: continue if a != t: i = min(d[a], d[t]) else: if d[a] == 1: continue i = d[a] // 2 result += i d[a] -= i d[t] -= i print(result)
from bisect import bisect_right n = int(eval(input())) d = {} for a in map(int, input().split()): if a in d: d[a] += 1 else: d[a] = 1 alist = list(sorted(list(d.keys()), reverse = True)) amax = alist[0] t = 1 power_of_two = [] while t < 2 * amax: t *= 2 power_of_two.append(t) result = 0 for a in alist: if a not in d: continue t = power_of_two[bisect_right(power_of_two, a)] - a if t not in d: continue if a != t: i = min(d[a], d[t]) else: if d[a] == 1: continue i = d[a] // 2 result += i if d[a] == i: del d[a] else: d[a] -= i if d[t] == i: del d[t] else: d[t] -= i print(result)
p03201
from bisect import bisect_right n = int(eval(input())) d = {} for a in map(int, input().split()): if a in d: d[a] += 1 else: d[a] = 1 alist = list(sorted(list(d.keys()), reverse = True)) amax = alist[0] t = 1 power_of_two = [] while t < 2 * amax: t *= 2 power_of_two.append(t) result = 0 for a in alist: if a not in d: continue t = power_of_two[bisect_right(power_of_two, a)] - a if t not in d: continue if a != t: i = min(d[a], d[t]) else: if d[a] == 1: continue i = d[a] // 2 result += i if d[a] == i: del d[a] else: d[a] -= i if d[t] == i: del d[t] else: d[t] -= i print(result)
from bisect import bisect_right n = int(eval(input())) d = {} for a in map(int, input().split()): if a in d: d[a] += 1 else: d[a] = 1 alist = list(sorted(list(d.keys()), reverse = True)) amax = alist[0] t = 1 power_of_two = [] while t < 2 * amax: t *= 2 power_of_two.append(t) result = 0 for a in alist: if a not in d: continue t = power_of_two[bisect_right(power_of_two, a)] - a if t not in d: continue if a != t: if d[a] < d[t]: i = d[a] else: i = d[t] else: if d[a] == 1: continue i = d[a] // 2 result += i if d[a] == i: del d[a] else: d[a] -= i if d[t] == i: del d[t] else: d[t] -= i print(result)
p03201
N = int(eval(input())) A_list = list(map(int, input().split())) two_list = [2 ** i for i in range(32)] count = 0 items = {} for a in A_list: if a in items: items[a] += 1 else: items[a] = 1 A_list.sort(reverse=True) for a in A_list: if items[a] >= 1: for t in two_list: m = t -a if m in items: if m == a and items[a] >= 2: count += 1 items[a] -= 1 items[m] -= 1 break elif m != a and items[m] >= 1 and items[a] >=1: count += 1 items[a] -= 1 items[m] -= 1 break print(count)
N = int(eval(input())) A_list = list(map(int, input().split())) two_list = [2 ** i for i in range(32)] count = 0 items = {} for a in A_list: if a in list(items.keys()): items[a] += 1 else: items[a] = 1 A_list.sort(reverse=True) import bisect for a in A_list: if items[a] >= 1: index = bisect.bisect_left(two_list, a) for t in two_list[index:]: m = t - a if m in list(items.keys()): if m == a and items[a] >= 2: count += 1 items[a] -= 2 break elif m != a and items[m] >= 1 and items[a] >= 1: count += 1 items[a] -= 1 items[m] -= 1 break print(count)
p03201
N = int(input().rstrip()) A = [int(s) for s in input().rstrip().split()] A.sort() squared = [2] tmp = 2 while tmp < 2 * 10**9: tmp *= 2 squared.append(tmp) pairs = [] for i in range(N): for j in range(i+1, N): if A[i] + A[j] in squared: pairs.append([A[i], A[j]]) adopt = [format(i, 'b').rjust(len(pairs), '0') for i in range(2**len(pairs))] max_pair = 0 for i in range(len(adopt)): num = [] for j in range(len(pairs)): if adopt[i][j] == '1': num.append(pairs[j][0]) num.append(pairs[j][1]) if len(num) == len(set(num)) and len(num) > max_pair: max_pair = len(num) // 2 print(max_pair)
N = int(input().rstrip()) A = [int(s) for s in input().rstrip().split()] A.sort() squared = [2] tmp = 2 while tmp < 2 * 10**9: tmp *= 2 squared.append(tmp) cnt = 0 while A: max_num = A.pop() for i in range(len(A)): if max_num + A[i] in squared: cnt += 1 A.pop(i) break print(cnt)
p03201
N=int(eval(input())) a=list(map(int,input().split())) a_=sorted(a,key=lambda x:-x) ans=0 from collections import Counter c=Counter(a_) for i in a_: if c[i]==0: continue c[i]-=1 for n in range(i): if i<2**n: t=n break elif i==2**n: t=n+1 break j=2**t-i if j in c and c[j]>0: ans+=1 c[j]-=1 print(ans)
N=int(eval(input())) a=list(map(int,input().split())) a_=sorted(a,key=lambda x:-x) ans=0 from collections import Counter c=Counter(a_) for i in a_: if c[i]==0: continue c[i]-=1 m=1<<i.bit_length() j=m-i if j in c and c[j]>0: ans+=1 c[j]-=1 print(ans)
p03201
N = int(eval(input())) A = [int(a) for a in input().split()] A.sort() used = [0]*N import bisect ans = 0 for i in range(N-1, -1, -1): if used[i] == 1: continue num = pow(2, len(bin(A[i]))-2) - A[i] idx = bisect.bisect_left(A, num) while idx < i and used[idx] == 1: idx += 1 if A[idx] == num and idx < i: ans += 1 used[idx] = 1 used[i] = 1 print(ans)
N = int(eval(input())) A = [int(a) for a in input().split()] dic = {} for a in A: if a in dic: dic[a] += 1 else: dic[a] = 1 A.sort(reverse=True) ans = 0 for a in A: if dic[a] == 0: continue dic[a] -= 1 num = pow(2, len(bin(a))-2) - a if num in dic and dic[num] > 0: ans += 1 dic[num] -= 1 print(ans)
p03201
import math N = int(eval(input())) a = list(map(int,input().split())) count = 0 a.sort() a.reverse() while(a!=[]): temp = a[0] a.pop(0) b=2**math.floor(math.log2(temp)) if a ==[]: break if 2*b-temp in a: count += 1 a.pop(a.index(2*b-temp)) print(count)
n = int(eval(input())) a = list(map(int, input().split())) a.sort() b = {} for i in range(len(a)): if a[i] in b: b[a[i]] += 1 else: b[a[i]] = 1 twins = [] now = 1 while now <= a[-1]: now *= 2 twins.append(now) for i in range(len(a) - 1, -1, -1): tmpa = 0 if b[a[i]] == 0: continue for j in range(len(twins) - 1, -1, -1): if twins[j] <= a[i]: break tmpa = twins[j] - a[i] if tmpa == a[i]: if b[tmpa] >= 2: b[tmpa] -= 2 elif tmpa in b and b[tmpa] != 0: b[tmpa] -= 1 b[a[i]] -= 1 countb = 0 for i in b: countb += b[i] print(((n - countb) // 2))
p03201
import queue N=int(eval(input())) A=sorted([int(i) for i in input().split()],reverse=True) B=[2**(len(bin(i))-2) for i in A] R=[0 for i in range(N)] D=dict() for i in range(N): if not(A[i] in D): D[A[i]]=queue.Queue() D[A[i]].put(i) ans=0 for i in range(N): if R[i]==1: continue if B[i]==2*A[i]: if A[i] in D: flag=0 x=0;y=0 if not(D[A[i]].empty()): x=D[A[i]].get() flag=1 if flag==1 and not(D[A[i]].empty()): y=D[A[i]].get() flag=2 if flag==2: R[x]=1 R[y]=1 ans+=1 else: if (B[i]-A[i]) in D: if not(D[B[i]-A[i]].empty()): it=D[A[i]].get() j=D[B[i]-A[i]].get() R[i]=1 R[j]=1 ans+=1 print(ans)
N=int(eval(input())) A=sorted([int(i) for i in input().split()],reverse=True) B=[2**(len(bin(i))-2) for i in A] D=dict() for i in range(N): if not(A[i] in D): D[A[i]]=1 else: D[A[i]]+=1 ans=0 for i in range(N): if D[A[i]]<=0: continue if B[i]==2*A[i]: if D[A[i]]>=2: D[A[i]]-=2 ans+=1 else: if (B[i]-A[i]) in D: if D[B[i]-A[i]]!=0: D[A[i]]-=1 D[B[i]-A[i]]-=1 ans+=1 print(ans)
p03201
# https://beta.atcoder.jp/contests/agc029/submissions/3804087 from collections import Counter N = int(eval(input())) a = list(map(int, input().split())) a = sorted(a, reverse=True) b = Counter(a) ans = 0 for t in a: if b[t] == 0: continue s = (1 << t.bit_length()) - t if t == s: ans += b[t] // 2 b[t] %= 2 else: u = min(b[s], b[t]) ans += u b[s] -= u b[t] -= u print(ans)
N = int(eval(input())) a = list(map(int, input().split())) a = sorted(a, reverse=True) cnt = {} for n in a: cnt[n] = cnt.get(n, 0) + 1 ans = 0 for n in a: g = 2 ** n.bit_length() s = g - n if n == s: pair = cnt.get(s, 0) // 2 ans += pair cnt[s] = cnt.get(s, 0) % 2 else: pair = min(cnt.get(n, 0), cnt.get(s, 0)) ans += pair cnt[n] = cnt.get(n, 0) - pair cnt[s] = cnt.get(s, 0) - pair print(ans)
p03201
import collections N=int(eval(input())) x=list(map(int,input().split())) c=collections.Counter(x) t=[2**i for i in range(len(str(bin(max(x)*2))[2:])+1)[::-1]] count=0 x.sort() for i in range(len(x))[::-1]: for j in range(len(t)): if t[j]-x[i] in list(c.keys()): if t[j] < x[i]: break if t[j]-x[i] == x[i]: if c[x[i]] >= 2: c[x[i]] -= 2 count +=1 else: if c[t[j]-x[i]]>0 and c[x[i]]>0: c[t[j]-x[i]] -= 1 c[x[i]] -= 1 count+=1 print(count)
import collections N=int(eval(input())) x=list(map(int,input().split())) c=collections.Counter(x) t=[2**i for i in range(len(str(bin(max(x)*2))[2:])+1)[::-1]] count=0 start=0 x.sort() for i in range(len(x))[::-1]: for j in range(start,len(t)): if t[j] > 2*x[i]: start = j if t[j] < x[i]: break if t[j]-x[i] in list(c.keys()): if t[j]-x[i] == x[i]: if c[x[i]] >= 2: c[x[i]] -= 2 count +=1 else: if c[t[j]-x[i]]>0 and c[x[i]]>0: c[t[j]-x[i]] -= 1 c[x[i]] -= 1 count+=1 print(count)
p03201
from bisect import bisect_left N = int(eval(input())) A = sorted(list(map(int,input().split()))) cnt = 0 hist = [0 for _ in range(N)] for i in range(N-1,0,-1): if hist[i]==0: a = A[i] k = 0 while 2**k<=a: k += 1 b = 2**k-a ind = bisect_left(A,b) if ind<i and A[ind]==b and hist[ind]==0: hist[i]=1 hist[ind]=1 cnt += 1 elif ind<i and A[ind]==b and hist[ind]==1: ind += 1 while A[ind]==b and ind<i: if hist[ind]==0: hist[i]=1 hist[ind]=1 cnt += 1 break ind += 1 print(cnt)
from bisect import bisect_left N = int(eval(input())) A = sorted(list(map(int,input().split()))) A = [[A[i],0] for i in range(N)] cnt = 0 for i in range(N-1,0,-1): if A[i][1]==0: a = A[i][0] A[i][1]=1 k = 0 while 2**k<=a: k += 1 b = 2**k-a ind = bisect_left(A,[b,1]) ind -= 1 if ind<i and A[ind]==[b,0]: A[ind][1]=1 cnt += 1 print(cnt)
p03201
import sys nbs = list(sorted(map(int, list(sys.stdin)[1].strip().split()))) used = set() count = 0 for k in reversed(list(range(1, 31))): p = 1 << k j = len(nbs) - 1 for i, a in enumerate(nbs): if i not in used: while j > i and (nbs[j] > p - a or j in used): j -= 1 if j <= i: break if nbs[j] == p - a: used.add(i) used.add(j) count += 1 print(count)
eval(input()) nbs = list(map(int, input().split())) answer = 0 count = {} for nb in nbs: count[nb] = count.get(nb, 0) + 1 for nb in sorted(list(count.keys()), reverse=True): searching = (1 << nb.bit_length()) - nb nb_count = count[nb] sr_count = count.get(searching, 0) if sr_count: if nb == searching: increment = nb_count // 2 else: increment = nb_count if nb_count < sr_count else sr_count count[searching] -= increment answer += increment print(answer)
p03201
from collections import Counter n = int(eval(input())) nums = sorted(list(map(int, input().split())), reverse=True) num_map = Counter(nums) answer = 0 for num in nums: pair = (1 << (num.bit_length())) - num if num_map[num] > 0 and num_map[pair]: if num == pair and num_map[num] > 1: answer += 1 num_map[num] -= 2 if num > pair: answer += 1 num_map[num] -= 1 num_map[pair] -= 1 print(answer)
from collections import Counter n = int(eval(input())) nums = sorted(list(map(int, input().split())), reverse=True) num_map = Counter(nums) answer = 0 for num in nums: pair = (1 << (num.bit_length())) - num if num_map[num] > 0 and num_map[pair]: if num == pair and num_map[num] > 1: answer += 1 num_map[num] -= 2 if not num == pair: answer += 1 num_map[num] -= 1 num_map[pair] -= 1 print(answer)
p03201
from collections import* from bisect import* p=[1] for _ in range(30):p+=p[-1]*2, n,*a=list(map(int,open(0).read().split())) d=Counter(a) c=0 for b in sorted(a)[::-1]: if d[b]<1:continue d[b]-=1 x=p[bisect(p,b)]-b if d[x]: d[x]-=1 c+=1 print(c)
from collections import* C=Counter(a:=sorted(map(int,[*open(r:=0)][1].split()))) for x in a[::-1]: if C[x]>0:y=2**x.bit_length()-x;C[x]-=1;r+=C[y]>0;C[y]-=1 print(r)
p03201
from collections import Counter N = int(eval(input())) A_arr = list(map(int, input().split())) A_arr = sorted(A_arr, reverse=True) C_arr = Counter(A_arr) count = 0 while len(A_arr) != 0: v_max = A_arr.pop(0) C_arr[v_max] -= 1 v_pair = 2**v_max.bit_length() - v_max if C_arr[v_pair] != 0: A_arr.remove(v_pair) C_arr[v_pair] -= 1 count += 1 else: pass print(count)
from collections import Counter N = int(eval(input())) A_arr = list(map(int, input().split())) A_arr = sorted(A_arr, reverse=True) C_arr = Counter(A_arr) count = 0 for i in range(N): v_max = A_arr[i] if C_arr[v_max] == 0: continue C_arr[v_max] -= 1 v_pair = 2**v_max.bit_length() - v_max if C_arr[v_pair] > 0: C_arr[v_pair] -= 1 count += 1 else: pass print(count)
p03201
n=int(eval(input())) lists=list(map(int,input().split())) lists=sorted(lists,reverse=True) from collections import Counter dic=dict(Counter(lists)) ans=0 howmuch=[[] for i in range(30)] for num in lists: c=0 while num%2==0: num=num//2 c+=1 howmuch[c].append(num) for i in range(30): if len(howmuch[i])<=1: pass else: dic=dict(Counter(howmuch[i])) for some in howmuch[i]: for j in range(1,30): if 2**j-some in list(dic.keys()) and dic[some]>0 and dic[2**j-some]>0: if some!=2**(j-1): m=min(dic[some],dic[2**j-some]) dic[some]-=m dic[2**j-some]-=m ans+=m elif some==2**(j-1): ans+=dic[some]//2 dic[some]=dic[some]%2 print(ans)
from collections import defaultdict N = int(eval(input())) A = list(map(int, input().split())) ans = 0 D = defaultdict(int) for x in A: D[x] += 1 A.sort(reverse=True) for x in A: for i in range(1, 31): if D[x] > 0: if 2**i-x in list(D.keys()) and D[2**i-x] > 0: if 2**(i-1) != x: ans += 1 D[x] -= 1 D[2**i-x] -= 1 for i in range(31): ans += D[2**i]//2 print(ans)
p03201
import math,collections n=int(eval(input())) a=sorted(map(int,input().split()))[::-1] s=collections.Counter(a) r=0 f=0 for i,v in enumerate(a): if f:f=0;continue if s[v]==0:continue p=2**math.ceil(math.log2(v))-v if p in list(s.keys()) and s[p]>0:s[v]-=1;s[p]-=1;r+=1;continue if i<n-1 and p==0 and a[i+1]==v and s[v]>=2:s[v]-=2;r+=1;f=1 print(r)
import math,collections n=int(eval(input())) a=sorted(map(int,input().split()))[::-1] s=collections.Counter(a) r=0 f=0 for i,v in enumerate(a): p=2**math.ceil(math.log2(v))-v if f:f=0 elif s[v]==0:pass elif p in list(s.keys())and s[p]>0:s[v]-=1;s[p]-=1;r+=1 elif i<n-1 and p==0 and v==a[i+1]and s[v]>1:s[v]-=2;r+=1;f=1 print(r)
p03201
N = int(eval(input())) A = [int(_) for _ in input().split()] from collections import defaultdict xs = sorted(A, reverse=True) cs = defaultdict(int) for x in xs: cs[x] += 1 result = 0 for x in xs: if cs[x]: for i in range(31, 0, - 1): m = 2**i y = m - x if x == y: if cs[y] >= 2: cs[y] -= 2 result += 1 break else: if cs[y] >= 1: cs[x] -= 1 cs[y] -= 1 result += 1 break #print(cs) print(result)
N = int(eval(input())) A = [int(_) for _ in input().split()] xs = sorted(A, reverse=True) cs = {} for x in xs: cs[x] = cs.get(x, 0) + 1 result = 0 for x in xs: if cs[x]: for i in range(31, 0, - 1): m = 2**i y = m - x if y > x or y < 1: continue if y in cs: if x == y: if cs[y] >= 2: cs[y] -= 2 result += 1 break else: if cs[y] >= 1: cs[x] -= 1 cs[y] -= 1 result += 1 break #print(cs) print(result)
p03201
N=int(eval(input())) A=list(map(int,input().split())) import math import collections powers =[2**i for i in range(1,int(math.log(max(A),2)+2))] table=collections.defaultdict(int) table2=collections.defaultdict(set) ans=0 for i in range(N): table[A[i]]+=1 for x in table: for y in reversed(powers): if y - x in table: table2[x].add(y-x) table2 =list(table2.items()) table2.sort(key = lambda x:len(x[1])) for x,y in table2: while(table[x]>0): table[x]-=1 for z in y: if table[z]>0: ans+=1 table[z]-=1 break print(ans)
def main(): from collections import Counter import sys input = sys.stdin.buffer.readline _ = int(eval(input())) A = [int(i) for i in input().split()] ans = 0 c = Counter(A) B = sorted(list(c.keys()), reverse=True) for b in B: if c[b] == 0: continue a = (2 ** b.bit_length()) - b if a == b: cur = c[b]//2 else: if a in c: cur = min(c[a], c[b]) else: cur = 0 ans += cur # c[b] -= cur c[a] -= cur print(ans) if __name__ == '__main__': main()
p03201
#! /usr/bin/env python # -*- coding: utf-8 -*- # vim:fenc=utf-8 # """ agc 029 B """ from collections import Counter n = int(eval(input())) ali = list(map(int, input().split())) ali.sort(reverse=True) c = Counter(ali) ans = 0 def pow2(n): m = n tmp = 1 while m > 0: tmp = tmp << 1 m = m >> 1 return tmp ii = 0 m = ali[ii] while bool(c): n = pow2(m) - m c[m] -= 1 if c[m] == 0: c += Counter() ii += 1 m = ali[ii] if n in c: c[n] -= 1 ans += 1 c += Counter() print(ans)
#! /usr/bin/env python # -*- coding: utf-8 -*- # vim:fenc=utf-8 # """ agc 029 B """ from collections import Counter nn = int(eval(input())) ali = list(map(int, input().split())) ali.sort(reverse=True) c = Counter(ali) ans = 0 def pow2(n): m = n tmp = 1 while m > 0: tmp = tmp << 1 m = m >> 1 return tmp ii = 0 m = ali[ii] while bool(c): while c[ali[ii]] == 0: ii += 1 m = ali[ii] n = pow2(m) - m c[m] -= 1 if c[m] <= 0: del c[m] if n in c: c[n] -= 1 if c[n] <= 0: del c[n] ans += 1 print(ans)
p03201
import bisect """ def wantp(n): wq = [] while p[n] != n: wq.append(n) n = p[n] for i in wq: p[i] = n return n """ N = int(eval(input())) A = list(map(int,input().split())) A.sort() """ p = [i for i in range(N)] rank = [1] * N """ used = [False] * N ans = 0 for i in range(N): i = N - 1 - i now = 2 ** 33 while A[i] * 2 < now: now //= 2 na = A[-1] del A[-1] if used[i]: continue ind = bisect.bisect_left(A,now-na) while ind < i and used[ind] and na + A[ind] == now: ind += 1 if ind < i and na + A[ind] == now: used[ind] = True ans += 1 #print (now,na,ind) print (ans)
import bisect """ def wantp(n): wq = [] while p[n] != n: wq.append(n) n = p[n] for i in wq: p[i] = n return n """ N = int(eval(input())) B = list(map(int,input().split())) B.sort() A = [] AS = [] for i in range(N): if len(A) == 0: A.append(B[0]) AS.append(1) else: if A[-1] == B[i]: AS[-1] += 1 else: A.append(B[i]) AS.append(1) """ p = [i for i in range(N)] rank = [1] * N """ ans = 0 for i in range(len(A)): i = len(A) - 1 - i now = 2 ** 33 while A[i] * 2 < now: now //= 2 ind = bisect.bisect_left(A,now-A[i]) if A[ind] + A[i] == now: if ind == i: ans += AS[i] // 2 AS[i] = AS[i] % 2 else: ans += min(AS[i],AS[ind]) AS[ind] = max(AS[ind] - AS[i] , 0) #print (now,na,ind) print (ans)
p03201
N = int(eval(input())) A = list(map(int,input().split())) """ 2べきになる条件は? →片方が2の補数であること Nが大きい→貪欲的に 最大の奴はそれより小さい奴としかペアを組めない →大きい奴から貪欲にとっけいけばいける? いけそうかなぁ """ A.sort() An = [] for i in range(N): if i == 0 or An[-1][0] != A[i]: An.append([ A[i] , 1 ]) else: An[-1][1] += 1 import bisect ans = 0 #print (An) for i in range(len(An)-1,-1,-1): for j in range(An[i][1]): An[i][1] -= 1 for k in range(30,-1,-1): if 2**k <= An[i][0]: continue ind = bisect.bisect_left(An,[2**k - An[i][0],0]) if ind < len(An) and An[ind][0] == 2**k - An[i][0] and An[ind][1] > 0: An[ind][1] -= 1 ans += 1 break print (ans)
N = int(eval(input())) A = list(map(int,input().split())) """ 2べきになる条件は? →片方が2の補数であること Nが大きい→貪欲的に 最大の奴はそれより小さい奴としかペアを組めない →大きい奴から貪欲にとっけいけばいける? いけそうかなぁ """ A.sort() dic = {} lis = [] for i in range(N): if i == 0 or A[i-1] != A[i]: dic[A[i]] = 1 lis.append(A[i]) else: dic[A[i]] += 1 lis.reverse() ans = 0 d2lis = {} for i in range(32): d2lis[2**i] = i import math for i in lis: if i in d2lis: ans += dic[i]//2 dic[i] %= 2 else: t = 2 ** (int(math.log(i,2)) + 1) if t-i in dic: ans += min(dic[i],dic[t-i]) dic[t-i] = max(0, dic[t-i]-dic[i]) print (ans)
p03201
N = int(eval(input())) ball = list(map(int, input().split())) ball.sort(reverse = True) dic = {} for i in range(ball[0] + 1): dic[i] = 0 for x in ball: dic[x] += 1 ans = 0 for x in ball: if dic[x] != 0: y = 2**(x.bit_length()) - x if dic[y] != 0: if y == x and dic[y] == 1: continue ans += 1 dic[x] -= 1 dic[y] -= 1 print(ans)
N = int(eval(input())) ball = list(map(int, input().split())) ball.sort(reverse = True) ok = {} for x in ball: if x in ok: ok[x] += 1 else: ok[x] = 1 ans = 0 for x in ball: if ok[x] != 0: y = 2**(x.bit_length()) - x if y in ok: if ok[y] != 0: if y == x and ok[y] == 1: continue ans += 1 ok[x] -= 1 ok[y] -= 1 print(ans)
p03201
from collections import defaultdict n = int(eval(input())) a = list(map(int,input().split())) a.sort(key=lambda x:-x) d = defaultdict(int) for i in a: d[i] += 1 beki = [2**i for i in range(60,0,-1)] ans = 0 for i in a: if d[i] == 0: continue for j in beki: if j <= i: break ni = j - i if ni == i: if d[i] >= 2: ans +=1 d[i]-=2 break elif d[ni] > 0: ans += 1 d[ni]-=1 d[i]-=1 break print(ans)
from collections import defaultdict n = int(eval(input())) a = list(map(int,input().split())) a.sort(key=lambda x:-x) d = defaultdict(int) for i in a: d[i] += 1 beki = [2**i for i in range(32,0,-1)] ans = 0 for i in a: if d[i] == 0: continue for j in beki: if j <= i: break ni = j - i if ni == i: if d[i] >= 2: ans +=1 d[i]-=2 break elif d[ni] > 0: ans += 1 d[ni]-=1 d[i]-=1 break print(ans)
p03201
import bisect from collections import Counter n = int(eval(input())) A = list(map(int,input().split())) A.sort() C = Counter(A) ans = 0 for i in range(n-1,-1,-1): temp = A[i] beki = 1 while beki <= temp: beki *= 2 counterpart = beki-temp if C[counterpart] > 0 and C[temp] > 0 and counterpart != temp: C[counterpart] -= 1 C[temp] -= 1 ans += 1 elif counterpart == temp and C[counterpart] > 1: C[counterpart] -= 2 ans += 1 print(ans)
from collections import Counter n = int(eval(input())) A = list(map(int,input().split())) A.sort() C = Counter(A) ans = 0 for i in range(n-1,-1,-1): temp = A[i] beki = 1 if C[temp] == 0: continue while beki <= temp: beki *= 2 counterpart = beki-temp if C[counterpart] > 0 and C[temp] > 0 and counterpart != temp: C[counterpart] -= 1 C[temp] -= 1 ans += 1 elif counterpart == temp and C[counterpart] > 1: C[counterpart] -= 2 ans += 1 print(ans)
p03201
import sys from collections import Counter read = sys.stdin.read readline = sys.stdin.readline N, *A = list(map(int, read().split())) A.sort() a = Counter(A) answer = 0 # 可能な最大の2冪 limit = 1 << ((2 * 10 ** 9 + 1).bit_length() - 1) while A: n = A.pop() if a[n] == 0: continue a[n] -= 1 s = limit while n < s: b = s - n if a[b] > 0: answer += 1 a[b] -= 1 break s //= 2 print(answer)
import sys from collections import Counter read = sys.stdin.read readline = sys.stdin.readline N, *A = list(map(int, read().split())) A.sort() a = Counter(A) answer = 0 while A: n = A.pop() if a[n] == 0: continue a[n] -= 1 # 実現可能な最大の2冪 s = 1 << (2 * n).bit_length() while n < s: b = s - n if a[b] > 0: answer += 1 a[b] -= 1 break s //= 2 print(answer)
p03201
import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, random, time, copy, functools sys.setrecursionlimit(10**7) inf = 10 ** 20 eps = 1.0 / 10**10 mod = 10**9+7 dd = [(-1, 0), (0, 1), (1, 0), (0, -1)] ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)] def LI(): return [int(x) for x in sys.stdin.readline().split()] def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()] def LF(): return [float(x) for x in sys.stdin.readline().split()] def LS(): return sys.stdin.readline().split() def I(): return int(sys.stdin.readline()) def F(): return float(sys.stdin.readline()) def S(): return input() def pf(s): return print(s, flush=True) def main(): A, B, Q = LI() s, t, x = [], [], [] [s.append(I()) for i in range(A)] [t.append(I()) for i in range(B)] [x.append(I()) for i in range(Q)] # print(A, B,Q,s,t,x) # A = 10**5 # B = 10 ** 5 # Q = 10**5 for start in x: # print('\nstart', start) first_temple = [temple for temple in t if temple <= start] if len(first_temple) != 0: first_temple = first_temple[-1:] second_temple = [temple for temple in t if temple > start] if len(second_temple) != 0: second_temple = second_temple[:1] temples = first_temple + second_temple # print('temples', temples) first_shrine = [sh for sh in s if sh <= start] if len(first_shrine) != 0: first_shrine = first_shrine[-1:] second_shrine = [sh for sh in s if sh > start] if len(second_shrine) != 0: second_shrine = second_shrine[:1] shrines = first_shrine + second_shrine # print('shrines', shrines) result = 10**11 for temple in temples: for shrine in shrines: # print('temple/shrine', temple, shrine) near = min(abs(start - temple), abs(start - shrine)) far = max(abs(start - temple), abs(start - shrine)) if (temple < start and shrine < start) or (temple > start and shrine > start): result = min(result, far) elif (temple < start and shrine > start) or (temple > start and shrine < start): result = min(result, near * 2 + far) # print('on-going result', result) # print('result', result) print(result) if __name__ == "__main__": main()
import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, random, time, copy, functools sys.setrecursionlimit(10**7) inf = 10 ** 20 eps = 1.0 / 10**10 mod = 10**9+7 dd = [(-1, 0), (0, 1), (1, 0), (0, -1)] ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)] def LI(): return [int(x) for x in sys.stdin.readline().split()] def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()] def LF(): return [float(x) for x in sys.stdin.readline().split()] def LS(): return sys.stdin.readline().split() def I(): return int(sys.stdin.readline()) def F(): return float(sys.stdin.readline()) def S(): return input() def pf(s): return print(s, flush=True) def main(): A, B, Q = LI() s, t, x = [], [], [] [s.append(I()) for i in range(A)] [t.append(I()) for i in range(B)] [x.append(I()) for i in range(Q)] s = [-10**18] + s + [10**18] t = [-10**18] + t + [10**18] # print(A, B,Q,s,t,x) # A = 10**5 # B = 10 ** 5 # Q = 10**5 # [s.append(i) for i in range(A)] # [t.append(i) for i in range(B)] # [x.append(i) for i in range(Q)] # s = [-10**18] + s + [10**18] # t = [-10**18] + t + [10**18] # print(len(s), len(t), s[0], s[-1]) for start in x: # first_temple = [temple for temple in t if temple <= start] first_temple_idx = bisect.bisect_left(t, start) - 1 second_temple_idx = first_temple_idx + 1 temples = [first_temple_idx, second_temple_idx] first_shrine_idx = bisect.bisect_left(s, start) - 1 second_shrine_idx = first_shrine_idx + 1 shrines = [first_shrine_idx, second_shrine_idx] # print(temples, shrines) # if len(first_temple) != 0: # first_temple = first_temple[-1:] # second_temple = [temple for temple in t if temple > start] # if len(second_temple) != 0: # second_temple = second_temple[:1] # temples = first_temple + second_temple # # print('temples', temples) # first_shrine = [sh for sh in s if sh <= start] # if len(first_shrine) != 0: # first_shrine = first_shrine[-1:] # second_shrine = [sh for sh in s if sh > start] # if len(second_shrine) != 0: # second_shrine = second_shrine[:1] # shrines = first_shrine + second_shrine # print('shrines', shrines) result = 10**11 for temple in temples: for shrine in shrines: # print('temple/shrine', temple, shrine) # print(temple, shrine) near = min(abs(start - t[temple]), abs(start - s[shrine])) far = max(abs(start - t[temple]), abs(start - s[shrine])) if (t[temple] < start and s[shrine] < start) or (t[temple] > start and s[shrine] > start): result = min(result, far) elif (t[temple] < start and s[shrine] > start) or (t[temple] > start and s[shrine] < start): result = min(result, near * 2 + far) # print('on-going result', result) # print('result', result) print(result) if __name__ == "__main__": main()
p03112
# input A, B, Q = list(map(int, input().split())) S, T, X = [], [], [] for _ in range(A): S.append(int(eval(input()))) for _ in range(B): T.append(int(eval(input()))) for _ in range(Q): X.append(int(eval(input()))) # end of world EOW = 10 ** 10 # boundary info tbounds = [(T[i+1] + T[i]) // 2 for i in range(B-1)] sbounds = [(S[i+1] + S[i]) // 2 for i in range(A-1)] # add pivot tbounds.insert(0, 0) tbounds.append(EOW) sbounds.insert(0, 0) sbounds.append(EOW) # temple to shrine ttos = [] cur = 0 for i in range(B): while True: if T[i] <= sbounds[cur]: break cur += 1 ttos.append(cur-1) # shrine to temple stot = [] cur = 0 for i in range(A): while True: if S[i] <= tbounds[cur]: break cur += 1 stot.append(cur-1) for x in X: snear = 0 while True: if x <= sbounds[snear]: break snear += 1 tnears_range = list(range(max(0, stot[snear-1]), min(stot[snear-1] + 4, B))) best = abs(S[snear-1]-x) + abs(T[stot[snear-1]]-S[snear-1]) for j in tnears_range: dist = abs(T[j]-x) + abs(S[ttos[j]]-T[j]) if dist < best: best = dist # svisit = 0 # tnear = 0 # best = EOW * 2 # # search # for i in range(A): # tnear = stot[i] # dist = abs(S[i]-x) + abs(T[tnear]-S[i]) # if best > dist: # best = dist # else: # break # for i in range(max(0, tnear-3), min(tnear+3, B)): # snear = ttos[i] # dist = abs(T[i]-x) + abs(S[snear]-T[i]) # if best > dist: # best = dist print(best) # print(min(min(abs(S[i]-x) + abs(T[nt]-S[i]) for (i, nt) in enumerate(stot)), min(abs(T[i]-x) + abs(S[ns]-T[i]) for (i, ns) in enumerate(ttos))))
import bisect A, B, Q = list(map(int, input().split())) INF = 10 ** 18 s = [-INF] + [int(eval(input())) for i in range(A)] + [INF] t = [-INF] + [int(eval(input())) for i in range(B)] + [INF] for q in range(Q): x = int(eval(input())) b, d = bisect.bisect_right(s, x), bisect.bisect_right(t, x) res = INF for S in [s[b - 1], s[b]]: for T in [t[d - 1], t[d]]: d1, d2 = abs(S - x) + abs(T - S), abs(T - x) + abs(S - T) res = min(res, d1, d2) print(res)
p03112
import bisect a,b,q=list(map(int,input().split())) s=[-999999999999]+[int(eval(input())) for i in range(a)]+[999999999999] t=[-999999999999]+[int(eval(input())) for i in range(b)]+[999999999999] x=[int(eval(input())) for i in range(q)] for i in range(q): a1=s[bisect.bisect_left(s,x[i])-1] a2=s[bisect.bisect_left(s,x[i])] b1=t[bisect.bisect_left(t,x[i])-1] b2=t[bisect.bisect_left(t,x[i])] l=[] for j in [a1,a2]: for k in [b1,b2]: l.append(abs(j-k)+min(abs(x[i]-j),abs(x[i]-k))) print((min(l)))
import bisect import sys input=sys.stdin.readline a,b,q=list(map(int,input().split())) s=[-999999999999]+[int(eval(input())) for i in range(a)]+[999999999999] t=[-999999999999]+[int(eval(input())) for i in range(b)]+[999999999999] x=[int(eval(input())) for i in range(q)] for i in range(q): a1=s[bisect.bisect_left(s,x[i])-1] a2=s[bisect.bisect_left(s,x[i])] b1=t[bisect.bisect_left(t,x[i])-1] b2=t[bisect.bisect_left(t,x[i])] l=[] for j in [a1,a2]: for k in [b1,b2]: l.append(abs(j-k)+min(abs(x[i]-j),abs(x[i]-k))) print((min(l)))
p03112
import sys def main(): input = sys.stdin.readline a, b, q = list(map(int, input().split())) s = [0]*a for i in range(a): s[i] = int(eval(input())) t = [0]*b for i in range(b): t[i] = int(eval(input())) s_near = [0]*a k = 0 for i in range(a): if k == b-1: s_near[i] = abs(s[i]-t[b-1]) continue while True: sub = min([abs(s[i]-t[k]), abs(s[i]-t[k+1])]) if sub == abs(s[i]-t[k+1]): k += 1 if k == b-1: s_near[i] = sub break continue else: s_near[i] = sub break t_near = [0]*b k = 0 for i in range(b): if k == a-1: t_near[i] = abs(t[i]-s[a-1]) continue while True: sub = min([abs(t[i]-s[k]), abs(t[i]-s[k+1])]) if sub == abs(t[i]-s[k+1]): k += 1 if k == a-1: t_near[i] = sub break continue else: t_near[i] = sub break for i in range(q): x = int(eval(input())) if x <= s[0]: sub1 = s[0]-x+s_near[0] elif x >= s[a-1]: sub1 = x-s[a-1]+s_near[a-1] else: l, r = 0, a while r-l > 1: k = (r+l)//2 if x <= s[k]: r = k else: l = k sub1 = min([s[r]-x+s_near[r], x-s[l]+s_near[l]]) if x <= t[0]: sub2 = t[0]-x+t_near[0] elif x >= t[b-1]: sub2 = x-t[b-1]+t_near[b-1] else: l, r = 0, b while r-l > 1: k = (r+l)//2 if x <= t[k]: r = k else: l = k sub2 = min([t[r]-x+t_near[r], x-t[l]+t_near[l]]) ans = min([sub1, sub2]) print(ans) if __name__ == "__main__": main()
import sys import bisect def main(): input = sys.stdin.readline inf = pow(10, 18) a, b, q = list(map(int, input().split())) s = [-inf]+[int(eval(input())) for _ in range(a)]+[inf] t = [-inf]+[int(eval(input())) for _ in range(b)]+[inf] for _ in range(q): x = int(eval(input())) i = bisect.bisect_left(s, x) j = bisect.bisect_left(t, x) ans = inf for l in range(-1, 1): for m in range(-1, 1): sub = min([abs(x-s[i+l]), abs(x-t[j+m])])+abs(s[i+l]-t[j+m]) if ans > sub: ans = sub print(ans) if __name__ == "__main__": main()
p03112
import bisect A, B, Q = list(map(int, input().split())) ss = [int(eval(input())) for _ in range(A)] ts = [int(eval(input())) for _ in range(B)] xs = [int(eval(input())) for _ in range(Q)] ss.sort() ts.sort() for x in xs: i = bisect.bisect_left(ss, x) s1 = ss[i - 1] if 0 < i else -(10 ** 20) s2 = ss[i] if i < A else 10 ** 20 i = bisect.bisect_left(ts, x) t1 = ts[i - 1] if 0 < i else -(10 ** 20) t2 = ts[i] if i < B else 10 ** 20 # s1 < x < s2 # t1 < x < t2 print((min( x - min(s1, t1), # s1, t1 2 * (x - s1) + (t2 - x), # s1, t2 2 * (t2 - x) + (x - s1), # t2, s1 2 * (s2 - x) + (x - t1), # s2, t1 2 * (x - t1) + (s2 - x), # t1, s2 max(s2, t2) - x, # s2, t2 )))
import bisect import io import sys LINES = sys.stdin.read().splitlines()[::-1] def readline(): return LINES.pop() A, B, Q = list(map(int, readline().split())) ss = [int(readline()) for _ in range(A)] ts = [int(readline()) for _ in range(B)] xs = [int(readline()) for _ in range(Q)] ss.sort() ts.sort() for x in xs: i = bisect.bisect_left(ss, x) s1 = ss[i - 1] if 0 < i else -(10 ** 20) s2 = ss[i] if i < A else 10 ** 20 i = bisect.bisect_left(ts, x) t1 = ts[i - 1] if 0 < i else -(10 ** 20) t2 = ts[i] if i < B else 10 ** 20 # s1 < x < s2 # t1 < x < t2 print((min( x - min(s1, t1), # s1, t1 2 * (x - s1) + (t2 - x), # s1, t2 2 * (t2 - x) + (x - s1), # t2, s1 2 * (s2 - x) + (x - t1), # s2, t1 2 * (x - t1) + (s2 - x), # t1, s2 max(s2, t2) - x, # s2, t2 )))
p03112
import sys input = sys.stdin.readline class AvlTree: """ Avl木を表現するクラス """ def __init__(self): self.avl = [None,None,None,0] def node(self,key): return [None,key,None,0] # search(x) xが存在するならTrueをしないならFalseを返す def search(self,key,avl=None): if avl == None: avl = self.avl if key == avl[1]: return True elif key < avl[1]: if avl[0] == None: return False else: return self.search(key,avl[0]) elif avl[1] < key: if avl[2] == None: return False else: return self.search(key,avl[2]) def search_lower(self,key,key_lower=None,avl=0): if avl == 0: avl = self.avl if avl == None: return key_lower elif key < avl[1]: if avl[0] == None: return key_lower else: return self.search_lower(key,key_lower,avl[0]) elif avl[1] < key: key_lower = avl[1] if avl[2] == None: return key_lower else: return self.search_lower(key,key_lower,avl[2]) #avl[1] == keyの場合 if avl[0] == None: return key_lower else: if key_lower == None: return self.end_higher(avl[0][1],avl[0]) else: return max(key_lower,self.end_higher(avl[0][1],avl[0])) def search_higher(self,key,key_higher=None,avl=0): if avl == 0: avl = self.avl if avl == None: return key_higher if key < avl[1]: key_higher = avl[1] if avl[0] == None: return key_higher else: return self.search_higher(key,key_higher,avl[0]) if avl[1] < key: if avl[2] == None: return key_higher else: return self.search_higher(key,key_higher,avl[2]) #self.key == keyの場合 if avl[2] == None: return key_higher else: if key_higher == None: return self.end_lower(avl[2][1],avl[2]) else: return min(key_higher,self.end_lower(avl[2][1],avl[2])) def end_lower(self,end_lower_key,avl=None): if avl == None: avl = self.avl if avl[0] == None: return end_lower_key else: end_lower_key = avl[0][1] return self.end_lower(end_lower_key,avl[0]) def end_higher(self,end_higher_key,avl=None): if avl == None: avl = self.avl if avl[2] == None: return end_higher_key else: end_higher_key = avl[2][1] return self.end_higher(end_higher_key,avl[2]) def DoubleRightRotation(self,avl): tl = avl[0] avl[0] = tl[2][2] tl[2][2] = avl # selfはそのノード tlr = tl[2] tl[2] = tlr[0] tlr[0] = tl if tlr[3] == 1: tlr[2][3] = 2 tlr[0][3] = 0 elif tlr[3] == 2: tlr[2][3] = 0 tlr[0][3] = 1 elif tlr[3] == 0: tlr[2][3] = 0 tlr[0][3] = 0 tlr[3] = 0 return tlr def DoubleLeftRotation(self,avl): tr = avl[2] avl[2] = tr[0][0] tr[0][0] = avl trl = tr[0] tr[0] = trl[2] trl[2] = tr if trl[3] == 2: trl[0][3] = 1 trl[2][3] = 0 elif trl.balance == 1: trl[0][3] = 0 trl[2][3] = 2 elif trl.balance == 0: trl[0][3] = 0 trl[2][3] = 0 trl[3] = 0 return trl def SingleLeftRotation(self,avl): tr = avl[2] tr[3] = 0 avl[3] = 0 avl[2] = tr[0] tr[0] = avl return tr def SingleRightRotation(self,avl): tl = avl[0] tl[3] = 0 avl[3] = 0 avl[0] = tl[2] tl[2] = avl return tl def replace(self,p,v,avl): # 親ノードpの下にある自分(avl)をvに置き換える。 if p[0] is avl: p[0] = v else : p[2] = v def insert(self,key): # rootでのみ呼ばれる挿入 if self.avl[1] == None: # rootを含むrotationはしないことにする。 self.avl[1] = key return self.avl if key < self.avl[1]: if self.avl[0] == None: self.avl[0] = self.node(key) else: self.insertx(self.avl,key,self.avl[0]) elif self.avl[1] < key: if self.avl[2] == None: self.avl[2] = self.node(key) else: self.insertx(self.avl,key,self.avl[2]) else: # key == self.avl[1]: pass # do not overwrite def insertx(self,p,key,avl): # replaceを呼ぶために一つ上の親を持っているinsert if key < avl[1]: if avl[0] == None: avl[0] = self.node(key) else: if not self.insertx(avl,key,avl[0]): # 左の木が生長しなければ、 return False # 成長しない balance = avl[3] if balance == 2: avl[3] = 0 return False elif balance == 0: avl[3] = 1 return True # 成長した elif balance == 1: if avl[0][3] == 2: self.replace(p,self.DoubleRightRotation(avl),avl) elif self.left.balance == 1: self.replace(p,self.SingleRightRotation(avl),avl) return False # rotationを行うと成長しない if avl[1] < key: if avl[2] == None: avl[2] = self.node(key) else: if not self.insertx(avl,key,avl[2]): return False balance = avl[3] if balance == 1: avl[3] = 0 return False elif balance == 0: avl[3] = 2 return True elif balance == 2: if avl[2][3] == 1: self.replace(p,self.DoubleLeftRotation(avl),avl) elif avl[2][3] == 2: self.replace(p,self.SingleLeftRotation(avl),avl) return False return False # avl[1] == keyの時は何もしないので成長もしない def debug(self): print((self.avl)) def main(): a, b, q = list(map(int,input().split())) treeA, treeB = AvlTree(), AvlTree() INF = 10**12 for _ in range(a): treeA.insert(int(eval(input()))) for _ in range(b): treeB.insert(int(eval(input()))) for _ in range(q): i = int(eval(input())) p1,p2 = 0, 0 alow = treeA.search_lower(i) ahigh = treeA.search_higher(i) p1 = abs(alow-i) if alow else INF p2 = abs(ahigh-i) if ahigh else INF if alow: add = INF if treeB.search_lower(alow): add = min(abs(treeB.search_lower(alow)-alow),add) if treeB.search_higher(alow): add = min(abs(treeB.search_higher(alow)-alow),add) p1 += add if ahigh: add = INF if treeB.search_lower(ahigh): add = min(abs(treeB.search_lower(ahigh)-ahigh),add) if treeB.search_higher(ahigh): add = min(abs(treeB.search_higher(ahigh)-ahigh),add) p2 += add ans = min(p1,p2) # btoa p3,p4 = 0, 0 blow = treeB.search_lower(i) bhigh = treeB.search_higher(i) p3 = abs(blow-i) if blow else INF p4 = abs(bhigh-i) if bhigh else INF if blow: add = INF if treeA.search_lower(blow): add = min(abs(treeA.search_lower(blow)-blow),add) if treeA.search_higher(blow): add = min(abs(treeA.search_higher(blow)-blow),add) p3 += add if bhigh: add = INF if treeA.search_lower(bhigh): add = min(abs(treeA.search_lower(bhigh)-bhigh),add) if treeA.search_higher(bhigh): add = min(abs(treeA.search_higher(bhigh)-bhigh),add) p4 += add ans = min(p3,p4,ans) print(ans) if __name__ == "__main__": main()
import bisect A, B, Q = list(map(int,input().split())) INF = 10**18 s = [-INF] + [int(eval(input())) for i in range(A)] + [INF] t = [-INF] + [int(eval(input())) for i in range(B)] + [INF] x = [int(eval(input())) for i in range(Q)] for e in x: b, d = bisect.bisect_right(s,e), bisect.bisect_right(t,e) a, c = b-1, d-1 ans = INF for S in [s[a],s[b]]: for T in [t[c],t[d]]: d1, d2 = abs(S-e) + abs(T-S), abs(T-e) + abs(S-T) ans = min(ans, d1, d2) print(ans)
p03112
read = input # read = Read(False, """ # 1 1 3 # 1 # 10000000000 # 2 # 9999999999 # 5000000000 # """).read A,B,Q = list(map(int, read().split(' '))) arrA = list(map(int, [read() for i in range(A)])) arrB = list(map(int, [read() for i in range(B)])) arrQ = list(map(int, [read() for i in range(Q)])) arrA.sort() arrB.sort() import bisect def cs(x, arr): al = bisect.bisect_left(arr, x) return list([arr[i] for i in [i for i in range(al-1, al + 2) if i>=0 and i< len(arr)]]) def same_s(da,db): da = da / abs(da) db = db / abs(db) return da*db>0 def t(q,a,b): da = q - a db = q - b if(min(abs(da), abs(db)) == 0): return max(abs(da), abs(db)) if same_s(da, db): return max(abs(da), abs(db)) else : return 2*min(abs(da), abs(db)) + max(abs(da), abs(db)) for q in arrQ: optA = cs(q, arrA) optB = cs(q, arrB) ret = 10 ** 11 for a in optA: for b in optB: ret = min(ret, t(q, a,b)) print(ret)
import bisect read = input bisect_right = bisect.bisect_right # @ see https://docs.python.org/zh-cn/3/library/bisect.html a ,b , q = list(map(int, read().split())) INF = 10**18 A =[-INF] + [int(read()) for i in range(a)] +[INF] B =[-INF] + [int(read()) for i in range(b)] +[INF] A.sort() B.sort() try: for i in range(q): x = int(read()) sa = bisect_right(A, x) sb = bisect_right(B, x) ans = INF for pa in [A[sa - 1 ], A[sa]]: for pb in [B[sb - 1 ], B[sb]]: d1 = abs(x - pa) + abs(pa - pb) d2 = abs(x - pb) + abs(pb - pa) ans = min(ans, d1, d2) print(ans) except: print((x, sa, sb))
p03112
import bisect left_sentinel = -2 * (10 ** 10) right_sentinel = 3 * (10 ** 10) A, B, Q = list(map(int, input().split())) ss = [left_sentinel] + [int(eval(input())) for _ in range(A)] + [right_sentinel] ts = [left_sentinel] + [int(eval(input())) for _ in range(B)] + [right_sentinel] for _ in range(Q): x = int(eval(input())) s_index = bisect.bisect_left(ss, x) right_s = ss[s_index] left_s = ss[s_index - 1] t_index = bisect.bisect_left(ts, x) right_t = ts[t_index] left_t = ts[t_index - 1] ans = min( max(right_s, right_t) - x, right_s - left_t + min(right_s - x, x - left_t), right_t - left_s + min(right_t - x, x - left_s), x - min(left_s, left_t) ) print(ans)
import sys from bisect import * input = sys.stdin.buffer.readline INF = 10 ** 13 A, B, Q = list(map(int, input().split())) shrines = [-INF] + [int(eval(input())) for _ in range(A)] + [INF] temples = [-INF] + [int(eval(input())) for _ in range(B)] + [INF] queries = [int(eval(input())) for _ in range(Q)] for x in queries: s_index = bisect(shrines, x) t_index = bisect(temples, x) rs = shrines[s_index] rt = temples[t_index] ls = shrines[s_index - 1] lt = temples[t_index - 1] ans = INF ans = min(ans, max(rs, rt) - x) ans = min(ans, x - min(ls, lt)) ans = min(ans, (rt - ls)*2 - max(rt-x, x-ls)) ans = min(ans, (rs - lt)*2 - max(rs-x, x-lt)) print(ans)
p03112
import sys def input(): return sys.stdin.readline().strip() def mapint(): return list(map(int, input().split())) sys.setrecursionlimit(10**9) A, B, Q = mapint() Ss = [-10**20]+[int(eval(input())) for _ in range(A)]+[10**20] Ts = [-10**20]+[int(eval(input())) for _ in range(B)]+[10**20] Ss.sort() Ts.sort() from bisect import bisect_left for _ in range(Q): x = int(eval(input())) xs = bisect_left(Ss, x) xt = bisect_left(Ts, x) rr = max(Ss[xs]-x, Ts[xt]-x) ll = max(x-Ss[xs-1], x-Ts[xt-1]) lr = min(abs(x-Ss[xs-1])*2+abs(x-Ts[xt]), abs(x-Ts[xt-1])*2+abs(x-Ss[xs])) rl = min(abs(x-Ss[xs-1])+abs(x-Ts[xt])*2, abs(x-Ts[xt-1])+abs(x-Ss[xs])*2) print((min(rr, ll, rl, lr)))
import sys def input(): return sys.stdin.readline().strip() def mapint(): return list(map(int, input().split())) sys.setrecursionlimit(10**9) A, B, Q = mapint() Ss = [-10**18]+[int(eval(input())) for _ in range(A)]+[10**18] Ts = [-10**18]+[int(eval(input())) for _ in range(B)]+[10**18] from bisect import bisect_left for q in range(Q): x = int(eval(input())) ls = Ss[bisect_left(Ss, x)-1] lt = Ts[bisect_left(Ts, x)-1] rs = Ss[bisect_left(Ss, x)] rt = Ts[bisect_left(Ts, x)] print((min((rs-lt)+min(rs-x, x-lt), (rt-ls)+min(rt-x, x-ls), max(x-ls, x-lt), max(rs-x, rt-x))))
p03112
from bisect import bisect_right A, B, Q = list(map(int, input().split())) s = [int(eval(input())) for _ in range(A)] t = [int(eval(input())) for _ in range(B)] x = [int(eval(input())) for _ in range(Q)] INF = 10 ** 18 s = [-INF] + s + [INF] t = [-INF] + t + [INF] for i in range(Q): b = bisect_right(s, x[i]) d = bisect_right(t, x[i]) res = INF for n in [s[b - 1], s[b]]: for m in [t[d - 1], t[d]]: d1 = abs(n - x[i]) + abs(m - n) d2 = abs(m - x[i]) + abs(n - m) res = min(res, d1, d2) print(res)
from bisect import bisect_right A, B, Q = list(map(int, input().split())) INF = 10 ** 18 S = [-INF] + [int(eval(input())) for _ in range(A)] + [INF] T = [-INF] + [int(eval(input())) for _ in range(B)] + [INF] X = [int(eval(input())) for _ in range(Q)] for x in X: b = bisect_right(S, x) d = bisect_right(T, x) res = INF for s in [S[b - 1], S[b]]: for t in [T[d - 1], T[d]]: dist_0 = abs(x - s) + abs(s - t) dist_1 = abs(x - t) + abs(t - s) res = min(res, dist_0, dist_1) print(res)
p03112
from bisect import bisect_left INF = 10**18 A, B, Q = list(map(int, input().split())) S = [-INF] + [int(eval(input())) for _ in range(A)] + [INF] T = [-INF] + [int(eval(input())) for _ in range(B)] + [INF] for _ in range(Q): x = int(eval(input())) si = bisect_left(S,x) ti = bisect_left(T,x) res = INF res = min(res, max(S[si] - x, T[ti] - x)) res = min(res, max(x - S[si - 1], x - T[ti - 1])) res = min(res, x + T[ti] - 2 * S[si - 1]) res = min(res, -x + 2 * T[ti] - S[si - 1]) res = min(res, x + S[si] - 2 * T[ti - 1]) res = min(res, -x + 2 * S[si] - T[ti - 1]) print(res)
from bisect import bisect_left import sys input = sys.stdin.readline INF = 10**18 A, B, Q = list(map(int, input().split())) S = [-INF] + [int(eval(input())) for _ in range(A)] + [INF] T = [-INF] + [int(eval(input())) for _ in range(B)] + [INF] for _ in range(Q): x = int(eval(input())) si = bisect_left(S,x) ti = bisect_left(T,x) res = INF res = min(res, max(S[si] - x, T[ti] - x)) res = min(res, max(x - S[si - 1], x - T[ti - 1])) res = min(res, x + T[ti] - 2 * S[si - 1]) res = min(res, -x + 2 * T[ti] - S[si - 1]) res = min(res, x + S[si] - 2 * T[ti - 1]) res = min(res, -x + 2 * S[si] - T[ti - 1]) print(res)
p03112
from bisect import bisect_left A, B, Q = list(map(int, input().split())) S = [-float('inf')] + [int(eval(input())) for _ in range(A)] + [float('inf')] T = [-float('inf')] + [int(eval(input())) for _ in range(B)] + [float('inf')] S_rev = [-s for s in S][::-1] T_rev = [-t for t in T][::-1] for q in range(Q): x = int(eval(input())) ans = float('inf') # Case1: 右に直進 cost1_s = S[bisect_left(S, x)] - x cost1_t = T[bisect_left(T, x)] - x ans = min(ans, max(cost1_s, cost1_t)) # Case2: 左に直進 cost2_s = S_rev[bisect_left(S_rev, -x)] + x cost2_t = T_rev[bisect_left(T_rev, -x)] + x ans = min(ans, max(cost2_s, cost2_t)) # Case3: 右でS、左でT cost3_s = S[bisect_left(S, x)] - x cost3_t = T_rev[bisect_left(T_rev, -x)] + x ans = min(ans, 2 * min(cost3_s, cost3_t) + max(cost3_s, cost3_t)) # Case4: 右でT、左でS cost3_s = S_rev[bisect_left(S_rev, -x)] + x cost3_t = T[bisect_left(T, x)] - x ans = min(ans, 2 * min(cost3_s, cost3_t) + max(cost3_s, cost3_t)) print(ans)
def main(): from bisect import bisect_left A, B, Q = list(map(int, input().split())) INF = 2 * 10 ** 10 S = [-INF] + [int(eval(input())) for _ in range(A)] + [INF] T = [-INF] + [int(eval(input())) for _ in range(B)] + [INF] S_rev = [-s for s in S][::-1] T_rev = [-t for t in T][::-1] for q in range(Q): x = int(eval(input())) ans = 5 * 10 ** 10 # Case1: 右に直進 cost1_s = S[bisect_left(S, x)] - x cost1_t = T[bisect_left(T, x)] - x ans = min(ans, max(cost1_s, cost1_t)) # Case2: 左に直進 cost2_s = S_rev[bisect_left(S_rev, -x)] + x cost2_t = T_rev[bisect_left(T_rev, -x)] + x ans = min(ans, max(cost2_s, cost2_t)) # Case3: 右でS、左でT cost3_s = S[bisect_left(S, x)] - x cost3_t = T_rev[bisect_left(T_rev, -x)] + x ans = min(ans, 2 * min(cost3_s, cost3_t) + max(cost3_s, cost3_t)) # Case4: 右でT、左でS cost3_s = S_rev[bisect_left(S_rev, -x)] + x cost3_t = T[bisect_left(T, x)] - x ans = min(ans, 2 * min(cost3_s, cost3_t) + max(cost3_s, cost3_t)) print(ans) if __name__ == "__main__": main()
p03112
#!/usr/bin/env python3 from bisect import bisect_right def main(): A, B, Q = list(map(int, input().split())) s = [int(eval(input())) for _ in range(A)] t = [int(eval(input())) for _ in range(B)] # print(s,t) s = [-10**12] + s + [10**12] t = [-10**12] + t + [10**12] for _ in range(Q): x = int(eval(input())) i = bisect_right(s,x) j = bisect_right(t,x) # print(i,j,x) ans = [] ans.append(max(abs(s[i-1] - x), abs(t[j-1] - x))) ans.append(max(abs(s[i] - x), abs(t[j] - x))) ans.append(abs(s[i-1] - x) + abs(t[j] - x) + min(abs(s[i-1]-x), abs(t[j]-x))) ans.append(abs(s[i] - x) + abs(t[j-1] - x) + min(abs(s[i]-x), abs(t[j-1]-x))) print((min(ans))) if __name__ == "__main__": main()
from bisect import bisect_left A, B, Q = list(map(int, input().split())) INF = 10**12 s = [-INF] + [int(eval(input())) for _ in range(A)] + [INF] t = [-INF] + [int(eval(input())) for _ in range(B)] + [INF] for _ in range(Q): x = int(eval(input())) si = bisect_left(s,x) ti = bisect_left(t,x) sl = [s[si-1],s[si]] tl = [t[ti-1],t[ti]] ans = INF for i in range(2): for j in range(2): ans = min(ans, abs(x-sl[i])+abs(sl[i]-tl[j]), abs(x-tl[j])+abs(tl[j]-sl[i])) print(ans)
p03112
def calc(a_list, b_list, x): inf = 10**11 aa = [a - x for a in a_list] bb = [b - x for b in b_list] if len([b for b in bb if b>=0])*len([a for a in aa if a>=0]) >0: p_1 = max([b for b in bb if b>=0][0], [a for a in aa if a>=0][0]) else: p_1 = inf if len([b for b in bb if b<=0])*len([a for a in aa if a<=0]) >0: p_2 = max(abs(max([b for b in bb if b<=0])), abs(max([a for a in aa if a<=0]))) else: p_2 = inf if len([b for b in bb if b<=0])*len([a for a in aa if a>=0]) >0: b_tmp = max([b for b in bb if b<=0]) a_tmp = min([a for a in aa if a>=0]) p_3 = a_tmp - b_tmp + min(abs(a_tmp), abs(b_tmp)) else: p_3 = inf if len([b for b in bb if b>=0])*len([a for a in aa if a<=0]) >0: b_tmp = min([b for b in bb if b>=0]) a_tmp = max([a for a in aa if a<=0]) p_4 = b_tmp - a_tmp + min(abs(a_tmp), abs(b_tmp)) else: p_4 = inf return min(p_1, p_2, p_3, p_4) a,b,q = list(map(int, input().split())) a_list = [] b_list = [] q_list = [] for i in range(a): a_list.append(int(eval(input()))) for j in range(b): b_list.append(int(eval(input()))) for k in range(q): q = int(eval(input())) ans = calc(a_list, b_list, q) print(ans)
import bisect a, b, q = list(map(int, input().split())) s = [] for i in range(a): s.append(int(eval(input()))) t = [] for j in range(b): t.append(int(eval(input()))) inf = 10 ** 18 s = [-inf] + s + [inf] t = [-inf] + t + [inf] for k in range(q): x = int(eval(input())) s_r, t_r = bisect.bisect_right(s, x), bisect.bisect_right(t, x) res = inf for s_ in [s[s_r], s[s_r - 1]]: for t_ in [t[t_r], t[t_r - 1]]: d = min(abs(x-s_), abs(x-t_)) + abs(s_ - t_) res = min(d, res) print(res)
p03112
import bisect A, B, Q = list(map(int, input().split())) INF = 10 ** 11 s_list = [-INF] + [int(eval(input())) for i in range(A)] + [INF] t_list = [-INF] + [int(eval(input())) for i in range(B)] + [INF] for q in range(Q): x = int(eval(input())) near_right_s = bisect.bisect_right(s_list, x) near_right_t = bisect.bisect_right(t_list, x) min_distance = INF for s in [s_list[near_right_s - 1], s_list[near_right_s]]: for t in [t_list[near_right_t - 1], t_list[near_right_t]]: distance_1 = abs(s - x) + abs(t - s) distance_2 = abs(t - x) + abs(s - t) min_distance = min(min_distance, distance_1, distance_2) print(min_distance)
from sys import stdin nii=lambda:list(map(int,stdin.readline().split())) lnii=lambda:list(map(int,stdin.readline().split())) from bisect import bisect a,b,q=nii() s=[-10**18]+[int(eval(input())) for i in range(a)]+[10**18] t=[-10**18]+[int(eval(input())) for i in range(b)]+[10**18] for i in range(q): x=int(eval(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]: ans=min(ans,abs(i-x)+abs(j-i)) for i in [t_l,t_r]: for j in [s_l,s_r]: ans=min(ans,abs(i-x)+abs(j-i)) print(ans)
p03112
from sys import stdout printn = lambda x: stdout.write(str(x)) inn = lambda : int(eval(input())) inl = lambda: list(map(int, input().split())) inm = lambda: list(map(int, input().split())) ins = lambda : input().strip() DBG = True # and False BIG = 999999999999 R = 10**9 + 7 import bisect def ddprint(x): if DBG: print(x) a,b,q = inm() s = [] t = [] for i in range(a): s.append(inn()) for i in range(b): t.append(inn()) for qq in range(q): x = inn() mn = BIG i = bisect.bisect_left(s,x) ss = [0] if i==0 else [a-1] if i==a else [i-1,i] for y in ss: d = abs(s[y]-x) j = bisect.bisect_left(t,s[y]) tt = [0] if j==0 else [b-1] if j==b else [j-1,j] for z in tt: mn = min(mn, d+abs(s[y]-t[z])) i = bisect.bisect_left(t,x) tt = [0] if i==0 else [b-1] if i==b else [i-1,i] for y in tt: d = abs(t[y]-x) j = bisect.bisect_left(s,t[y]) ss = [0] if j==0 else [a-1] if j==a else [j-1,j] for z in ss: mn = min(mn, d+abs(t[y]-s[z])) print(mn)
from sys import stdout printn = lambda x: stdout.write(str(x)) inn = lambda : int(eval(input())) inl = lambda: list(map(int, input().split())) inm = lambda: list(map(int, input().split())) ins = lambda : input().strip() DBG = True # and False BIG = 999999999999 R = 10**9 + 7 import bisect def ddprint(x): if DBG: print(x) a,b,q = inm() s = [] t = [] for i in range(a): s.append(inn()) for i in range(b): t.append(inn()) for qq in range(q): x = inn() mn = BIG i = bisect.bisect_left(s,x) j = bisect.bisect_left(t,x) #ddprint("i {} j {}".format(i,j)) if i>0 and j>0: mn = min(mn, x-min(s[i-1],t[j-1])) if i>0 and j<b: mn = min(mn, min(x-s[i-1],t[j]-x)+t[j]-s[i-1]) if i<a and j>0: mn = min(mn, min(x-t[j-1],s[i]-x)+s[i]-t[j-1]) if i<a and j<b: mn = min(mn, max(s[i],t[j])-x) print(mn)
p03112
import sys from bisect import bisect_left def input(): return sys.stdin.readline().strip() def main(): A, B, Q = list(map(int, input().split())) S = [-10**100] T = [-10**100] for _ in range(A): S.append(int(eval(input()))) for _ in range(B): T.append(int(eval(input()))) S.append(10**100) T.append(10**100) for _ in range(Q): x = int(eval(input())) idx1 = bisect_left(S, x) idx2 = bisect_left(T, x) ans = 10 ** 100 cand1 = (x - S[idx1 - 1]) + abs(S[idx1 - 1] - T[idx2 - 1]) ans = min(ans, cand1) cand2 = (x - S[idx1 - 1]) + abs(S[idx1 - 1] - T[idx2]) ans = min(ans, cand2) cand3 = (S[idx1] - x) + abs(S[idx1] - T[idx2 - 1]) ans = min(ans, cand3) cand4 = (S[idx1] - x) + abs(S[idx1] - T[idx2]) ans = min(ans, cand4) cand5 = (x - T[idx2 - 1]) + abs(T[idx2 - 1] - S[idx1 - 1]) ans = min(ans, cand5) cand6 = (x - T[idx2 - 1]) + abs(T[idx2 - 1] - S[idx1]) ans = min(ans, cand6) cand7 = (T[idx2] - x) + abs(T[idx2] - S[idx1 - 1]) ans = min(ans, cand7) cand8 = (T[idx2] - x) + abs(T[idx2] - S[idx1]) ans = min(ans, cand8) print(ans) if __name__ == "__main__": main()
import sys from bisect import bisect_left def input(): return sys.stdin.readline().strip() def main(): A, B, Q = list(map(int, input().split())) S = [-10**50] T = [-10**50] for _ in range(A): S.append(int(eval(input()))) for _ in range(B): T.append(int(eval(input()))) S.append(10**50) T.append(10**50) for _ in range(Q): x = int(eval(input())) idx1 = bisect_left(S, x) idx2 = bisect_left(T, x) ans = 10 ** 11 cand1 = (x - S[idx1 - 1]) + abs(S[idx1 - 1] - T[idx2 - 1]) ans = min(ans, cand1) cand2 = (x - S[idx1 - 1]) + (T[idx2] - S[idx1 - 1]) ans = min(ans, cand2) cand3 = (S[idx1] - x) + (S[idx1] - T[idx2 - 1]) ans = min(ans, cand3) cand4 = (S[idx1] - x) + abs(S[idx1] - T[idx2]) ans = min(ans, cand4) cand5 = (x - T[idx2 - 1]) + abs(T[idx2 - 1] - S[idx1 - 1]) ans = min(ans, cand5) cand6 = (x - T[idx2 - 1]) + (S[idx1] - T[idx2 - 1]) ans = min(ans, cand6) cand7 = (T[idx2] - x) + (T[idx2] - S[idx1 - 1]) ans = min(ans, cand7) cand8 = (T[idx2] - x) + abs(T[idx2] - S[idx1]) ans = min(ans, cand8) print(ans) if __name__ == "__main__": main()
p03112
def main(): import bisect A, B, Q = list(map(int, input().split())) s = list() t = list() s.append(-10**12) t.append(-10**12) for i in range(A): s.append(int(eval(input()))) for i in range(B): t.append(int(eval(input()))) s.append(10**12) t.append(10**12) for i in range(Q): x = int(eval(input())) sw = bisect.bisect_right(s, x)-1 #xより西で一番近い(同じ場所でもいい)神社の番号 se = sw + 1 #xより東で一番近い神社の番号 tw = bisect.bisect_right(t, x)-1 te = tw + 1 l1 = max(x-s[sw], x-t[tw]) l2 = max(s[se]-x, t[te]-x) l3 = s[se]-x + s[se]-t[tw] l4 = t[te]-x + t[te]-s[sw] l5 = x-s[sw] + t[te]-s[sw] l6 = x-t[tw] + s[se]-t[tw] print((min(l1, l2, l3, l4, l5, l6))) main()
import bisect INF = 10**15 A, B, Q = list(map(int, input().split())) s = [-INF] + [int(eval(input())) for _ in range(A)] + [INF] t = [-INF] + [int(eval(input())) for _ in range(B)] + [INF] for i in range(Q): x = int(eval(input())) sw = bisect.bisect_right(s, x)-1 #xより西で一番近い(同じ場所でもいい)神社の番号 se = sw + 1 #xより東で一番近い神社の番号 tw = bisect.bisect_right(t, x)-1 te = tw + 1 res = INF for S in [s[sw], s[se]]: for T in [t[tw], t[te]]: d1, d2 = abs(x-S) + abs(T-S), abs(x-T) + abs(S-T) res = min(res, d1, d2) print(res)
p03112
A,B,Q = list(map(int,input().split())) S = [int(eval(input())) for _ in range(A)] T = [int(eval(input())) for _ in range(B)] X = [int(eval(input())) for _ in range(Q)] StoT_W = [10000000010]*A t = 0 for i in range(A): if S[i]<T[0]: continue while(((t+1)<B)and(T[t+1] < S[i])): t += 1 StoT_W[i] = S[i] - T[t] StoT_E = [10000000010]*A t = B-1 for i in range(A-1,-1,-1): if T[B-1]<S[i]: continue while((-1<(t-1))and(S[i] < T[t-1])): t-=1 StoT_E[i] = T[t] - S[i] StoT = [10000000010]*A for i in range(A): StoT[i] = min(StoT_W[i],StoT_E[i]) TtoS_W = [10000000010]*B s = 0 for i in range(B): if T[i]<S[0]: continue while(((s+1)<A)and(S[s+1] < T[i])): s += 1 TtoS_W[i] = T[i] - S[s] TtoS_E = [10000000010]*B s = 0 for i in range(B): while(S[s]<T[i]): s+=1 if s==A: break else: TtoS_E[i] = S[s] - T[i] continue break TtoS = [10000000010]*B for i in range(B): TtoS[i] = min(TtoS_W[i],TtoS_E[i]) for x in X: ans = 30000000000 for i in range(A): ans = min(ans,abs(S[i]-x)+StoT[i]) for i in range(B): ans = min(ans,abs(T[i]-x)+TtoS[i]) print(ans)
from bisect import bisect_left I = lambda x: [-1<<50] + [int(eval(input())) for i in range(x)] + [1<<50] A,B,Q = list(map(int,input().split())) S,T = I(A),I(B) for _ in range(Q): x = int(eval(input())) LS_ = bisect_left(S,x) LT_ = bisect_left(T,x) LS = S[LS_-1] RS = S[LS_] LT = T[LT_-1] RT = T[LT_] print((min( x-min(LS,LT), max(RS,RT)-x, (x-LS)*2+(RT-x), (x-LT)*2+(RS-x), (RT-x)*2+(x-LS), (RS-x)*2+(x-LT) )))
p03112
def main(): from bisect import bisect_left as bl a, b, q, *stx = list(map(int, open(0).read().split())) s = [-float('Inf')] + stx[:a] + [float('Inf')] t = [-float('Inf')] + stx[a:a + b] + [float('Inf')] x = stx[a + b:] tmp = [] for i in x: f, g = bl(s, i), bl(t, i) sl, sr = s[f - 1], s[f] tl, tr = t[g - 1], t[g] d = float('Inf') for j in [sl, sr]: for k in [tr, tl]: if j <= i and k <= i: d = min(d, i - min(j, k)) elif j <= i <= k or k <= i <= j: d = min(d, min(abs(i - j), abs(i - k)) * 2 + max(abs(i - j), abs(i - k))) else: d = min(d, max(j, k) - i) tmp.append(d) ans = '\n'.join(map(str, tmp)) print(ans) if __name__ == '__main__': main()
def main(): from bisect import bisect_left as bl a, b, q, *stx = list(map(int, open(0).read().split())) s = [-float('Inf')] + stx[:a] + [float('Inf')] t = [-float('Inf')] + stx[a:a + b] + [float('Inf')] x = stx[a + b:] tmp = [] for i in x: f, g = bl(s, i), bl(t, i) sr, sl = s[f - 1], s[f] tr, tl = t[g - 1], t[g] d = min([max(sl, tl) - i, i - min(sr, tr), 2 * (tl - sr) - max(i - sr, tl - i), 2 * (sl - tr) - max(sl - i, i - tr)]) tmp.append(d) ans = '\n'.join(map(str, tmp)) print(ans) if __name__ == '__main__': main()
p03112
import bisect a, b, q = list(map(int, input().split())) s = [int(eval(input())) for _ in range(a)] t = [int(eval(input())) for _ in range(b)] for i in range(q): x = int(eval(input())) idx_s = bisect.bisect_left(s, x) idx_t = bisect.bisect_left(t, x) indices_s = [] indices_t = [] if idx_s == 0: indices_s.append(0) elif idx_s == a: indices_s.append(a-1) else: indices_s.append(idx_s) indices_s.append(idx_s-1) if idx_t == 0: indices_t.append(0) elif idx_t == b: indices_t.append(b-1) else: indices_t.append(idx_t) indices_t.append(idx_t-1) ans = [] for i in indices_s: for j in indices_t: l, r = min(s[i], t[j]), max(s[i], t[j]) if x < l: ans.append(r-x) elif l <= x and x < r: ans.append(min(r-l+(x-l), r-l+(r-x))) else: ans.append(x-l) print((min(ans)))
import sys import bisect input = sys.stdin.readline a, b, q = list(map(int, input().split())) s = [int(eval(input())) for _ in range(a)] t = [int(eval(input())) for _ in range(b)] for i in range(q): x = int(eval(input())) idx_s = bisect.bisect_left(s, x) idx_t = bisect.bisect_left(t, x) indices_s = [] indices_t = [] if idx_s == 0: indices_s.append(0) elif idx_s == a: indices_s.append(a-1) else: indices_s.append(idx_s) indices_s.append(idx_s-1) if idx_t == 0: indices_t.append(0) elif idx_t == b: indices_t.append(b-1) else: indices_t.append(idx_t) indices_t.append(idx_t-1) ans = [] for i in indices_s: for j in indices_t: l, r = min(s[i], t[j]), max(s[i], t[j]) if x < l: ans.append(r-x) elif l <= x and x < r: ans.append(min(r-l+(x-l), r-l+(r-x))) else: ans.append(x-l) print((min(ans)))
p03112
from bisect import bisect_left as bl A, B, Q = list(map(int, input().split())) inf = 10 ** 11 Sh = [-1 * inf] + [int(eval(input())) for _ in range(A)] + [inf] Te = [-1 * inf] + [int(eval(input())) for _ in range(B)] + [inf] for _ in range(Q): q = int(eval(input())) temp = inf i = bl(Sh, q) que = [] que.append(i) if i != 0: que.append(i - 1) while len(que): j = que.pop() k = bl(Te, Sh[j]) temp = min(temp, abs(Sh[j] - Te[k]) + abs(Sh[j] - q)) if k != 0: temp = min(temp, abs(Sh[j] - Te[k - 1]) + abs(Sh[j] - q)) i = bl(Te, q) if abs(Te[i] - q) < temp: que.append(i) if abs(Te[i - 1] - q) < temp: que.append(i) if i != 0: que.append(i - 1) while len(que): j = que.pop() k = bl(Sh, Te[j]) temp = min(temp, abs(Te[j] - Sh[k]) + abs(Te[j] - q)) if k != 0: temp = min(temp, abs(Te[j] - Sh[k - 1]) + abs(Te[j] - q)) print(temp)
from bisect import bisect_left as bl A, B, Q = list(map(int, input().split())) inf = 10 ** 11 Sh = [-1 * inf] + [int(eval(input())) for _ in range(A)] + [inf] Te = [-1 * inf] + [int(eval(input())) for _ in range(B)] + [inf] for _ in range(Q): x = int(eval(input())) b, d = bl(Sh, x), bl(Te, x) res = inf for S in [Sh[b - 1], Sh[b]]: for T in [Te[d - 1], Te[d]]: d1, d2 = abs(S - x) + abs(T - S), abs(T - x) + abs(S - T) res = min(res, d1, d2) print(res)
p03112
import sys import bisect read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline in_n = lambda: int(readline()) in_nn = lambda: list(map(int, readline().split())) in_s = lambda: readline().rstrip().decode('utf-8') INF = 10**12 d = lambda a, b: abs(a - b) def main(): A, B, Q = in_nn() tmp = list(map(int, read().split())) s = tmp[:A] t = tmp[A:A + B] x = tmp[A + B:] a = [0] * Q for i in range(Q): n = x[i] if n <= s[0]: a1 = -INF a2 = s[0] elif n >= s[-1]: a1 = s[-1] a2 = INF else: si = bisect.bisect_right(s, n) a1 = s[si - 1] a2 = s[si] if n <= t[0]: b1 = -INF b2 = t[0] elif n >= t[-1]: b1 = t[-1] b2 = INF else: ti = bisect.bisect_right(t, n) b1 = t[ti - 1] b2 = t[ti] # print(a1, a2, b1, b2) ans = d(n, a1) + min(d(a1, b1), d(a1, b2)) ans = min(ans, d(n, a2) + min(d(a2, b1), d(a2, b2))) ans = min(ans, d(n, b1) + min(d(b1, a1), d(b1, a2))) ans = min(ans, d(n, b2) + min(d(b2, a1), d(b2, a2))) a[i] = ans print(('\n'.join(map(str, a)))) if __name__ == '__main__': main()
import sys import bisect read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline in_n = lambda: int(readline()) in_nn = lambda: list(map(int, readline().split())) in_s = lambda: readline().rstrip().decode('utf-8') INF = 10**12 def main(): A, B, Q = in_nn() tmp = list(map(int, read().split())) s = [-INF] + tmp[:A] + [INF] t = [-INF] + tmp[A:A + B] + [INF] x = tmp[A + B:] a = [0] * Q d = lambda a, b: abs(a - b) for i in range(Q): n = x[i] si = bisect.bisect_right(s, n) a1 = s[si - 1] a2 = s[si] ti = bisect.bisect_right(t, n) b1 = t[ti - 1] b2 = t[ti] ans = d(n, a1) + min(d(a1, b1), d(a1, b2)) ans = min(ans, d(n, a2) + min(d(a2, b1), d(a2, b2))) ans = min(ans, d(n, b1) + min(d(b1, a1), d(b1, a2))) ans = min(ans, d(n, b2) + min(d(b2, a1), d(b2, a2))) a[i] = ans print(('\n'.join(map(str, a)))) if __name__ == '__main__': main()
p03112
import bisect f_inf = float('inf') def cal(syurui, d, index): if d == 'e': return syurui[index] elif d == 'w': return syurui[index - 1] A, B, Q = list(map(int, input().split())) s = [int(eval(input())) for i in range(A)] s.sort() t = [int(eval(input())) for i in range(B)] t.sort() s.insert(0, -f_inf) t.insert(0, -f_inf) s.append(f_inf) t.append(f_inf) for i in range(Q): x = int(eval(input())) s_index = bisect.bisect_left(s, x) t_index = bisect.bisect_left(t, x) print((min(max(abs(x-s[s_index]), abs(x-t[t_index])), max(abs(x-s[s_index-1]), abs(x-t[t_index-1])), 2*abs(x-s[s_index-1])+abs(x-t[t_index]), 2*abs(x-s[s_index])+abs(x-t[t_index-1]), abs(x-s[s_index-1])+2*abs(x-t[t_index]), abs(x-s[s_index])+2*abs(x-t[t_index-1]))))
import bisect import sys f_inf = float('inf') def cal(syurui, d, index): if d == 'e': return syurui[index] elif d == 'w': return syurui[index - 1] A, B, Q = list(map(int, input().split())) s = [int(eval(input())) for i in range(A)] s.sort() t = [int(eval(input())) for i in range(B)] t.sort() s.insert(0, -f_inf) t.insert(0, -f_inf) s.append(f_inf) t.append(f_inf) for i in range(Q): x = int(sys.stdin.readline().rstrip('\n')) s_index = bisect.bisect_left(s, x) t_index = bisect.bisect_left(t, x) print((min(max(abs(x - s[s_index]), abs(x - t[t_index])), max(abs(x - s[s_index - 1]), abs(x - t[t_index - 1])), 2 * abs(x - s[s_index - 1]) + abs(x - t[t_index]), 2 * abs(x - s[s_index]) + abs(x - t[t_index - 1]), abs(x - s[s_index - 1]) + 2 * abs(x - t[t_index]), abs(x - s[s_index]) + 2 * abs(x - t[t_index - 1]))))
p03112
from bisect import bisect_left INF = 2 * 10**10 A, B, Q = map(int, input().split()) S = [int(input()) for _ in range(A)] T = [int(input()) for _ in range(B)] X = [int(input()) for _ in range(Q)] ans = [] for x in X: sr = bisect_left(S, x) sl = sr -1 if sr != len(S) and S[sr] == x: sl = sr tr = bisect_left(T, x) tl = tr - 1 if tr != len(T) and T[tr] == x: tl = tr ll = INF if sl == -1 or tl == -1 else x - min(S[sl], T[tl]) rr = INF if sr == len(S) or tr == len(T) else max(S[sr], T[tr]) - x lsrt = INF if sl == -1 or tr == len(T) else 2*(x-S[sl]) + (T[tr] - x) ltrs = INF if tl == -1 or sr == len(S) else 2*(x-T[tl]) + (S[sr] - x) rslt = INF if tl == -1 or sr == len(S) else 2*(S[sr] - x) + (x - T[tl]) rtls = INF if sl == -1 or tr == len(T) else 2*(T[tr] - x) + (x - S[sl]) ans.append(min(ll, rr, lsrt, ltrs, rslt, rtls)) print(*ans, sep='\n')
from bisect import bisect_left INF = 2 * 10**10 A, B, Q = map(int, input().split()) S = [-INF] + [int(input()) for _ in range(A)] + [INF] T = [-INF] + [int(input()) for _ in range(B)] + [INF] X = [int(input()) for _ in range(Q)] ans = [] for x in X: sr = bisect_left(S, x) sl = sr if S[sr] == x else sr -1 tr = bisect_left(T, x) tl = tr if T[tr] == x else tr -1 ll = x - min(S[sl], T[tl]) rr = max(S[sr], T[tr]) - x lsrt = 2*(x-S[sl]) + (T[tr] - x) ltrs = 2*(x-T[tl]) + (S[sr] - x) rslt = 2*(S[sr] - x) + (x - T[tl]) rtls = 2*(T[tr] - x) + (x - S[sl]) ans.append(min(ll, rr, lsrt, ltrs, rslt, rtls)) print(*ans, sep='\n')
p03112
from bisect import bisect_left def main(): a,b,q=list(map(int,input().split())) s=[int(eval(input())) for i in range(a)] t=[int(eval(input())) for i in range(b)] x=[int(eval(input())) for i in range(q)] inf=10**11 minf=-10**11 s=[minf]+s+[inf] t=[minf]+t+[inf] for i in range(q): ns=bisect_left(s,x[i]) nt=bisect_left(t,x[i]) ans=float('inf') for j in [s[ns],s[ns-1]]: for k in [t[nt],t[nt-1]]: ans=min(ans,min(abs(x[i]-j),abs(x[i]-k))+abs(max(j,k)-min(j,k))) print(ans) if __name__=='__main__': main()
from bisect import bisect_left import sys def main(): input=sys.stdin.readline a,b,q=list(map(int,input().split())) s=[int(eval(input())) for i in range(a)] t=[int(eval(input())) for i in range(b)] x=[int(eval(input())) for i in range(q)] inf=10**11 minf=-10**11 s=[minf]+s+[inf] t=[minf]+t+[inf] for i in range(q): ns=bisect_left(s,x[i]) nt=bisect_left(t,x[i]) ans=float('inf') for j in [s[ns],s[ns-1]]: for k in [t[nt],t[nt-1]]: ans=min(ans,min(abs(x[i]-j),abs(x[i]-k))+abs(max(j,k)-min(j,k))) print(ans) if __name__=='__main__': main()
p03112
import sys input = sys.stdin.readline import bisect INF = 1e+11 A,B,Q=list(map(int, input().split())) s = [-INF]+[int(eval(input())) for _ in range(A)]+[INF] t = [-INF]+[int(eval(input())) for _ in range(B)]+[INF] x = [int(eval(input())) for _ in range(Q)] for i in range(Q): a,b= bisect.bisect_right(s,x[i]), bisect.bisect_right(t,x[i]) ans = INF for S in [s[a-1], s[a]]: for T in [t[b-1], t[b]]: d1 = abs(S-x[i])+abs(S-T) d2 = abs(T-x[i])+abs(S-T) ans = min(ans, d1, d2) print(ans)
import sys input = sys.stdin.readline import bisect INF = 1e+11 A,B,Q=list(map(int, input().split())) s=[-INF]+[int(eval(input())) for _ in range(A)]+[INF] t=[-INF]+[int(eval(input())) for _ in range(B)]+[INF] x=[int(eval(input())) for _ in range(Q)] for i in range(Q): a,b=bisect.bisect_right(s,x[i]), bisect.bisect_right(t,x[i]) ans = INF for j in [s[a-1], s[a]]: for k in [t[b-1], t[b]]: d1 = abs(j-x[i])+abs(j-k) d2 = abs(k-x[i])+abs(j-k) ans = min(ans, d1, d2) print(ans)
p03112
import bisect a,b,q = list(map(int,input().split())) s = [int(eval(input())) for _ in range(a)] t = [int(eval(input())) for _ in range(b)] for i in range(q): x = int(eval(input())) xs = bisect.bisect(s,x) xt = bisect.bisect(t,x) ds = [] if 0 < xs < a: ds.append([s[xs-1],abs(x-s[xs-1])]) ds.append([s[xs],abs(x-s[xs])]) elif xs == a: ds.append([s[xs-1],abs(x-s[xs-1])]) else: ds.append([s[xs],abs(x-s[xs])]) dt = [] if 0 < xt < b: dt.append([t[xt-1],abs(x-t[xt-1])]) dt.append([t[xt],abs(x-t[xt])]) elif xt == b: dt.append([t[xt-1],abs(x-t[xt-1])]) else: dt.append([t[xt],abs(x-t[xt])]) ans = [] for j in range(len(ds)): for k in range(len(dt)): ans.append(ds[j][1] + abs(dt[k][0]-ds[j][0])) for j in range(len(dt)): for k in range(len(ds)): ans.append(dt[j][1] + abs(ds[k][0]-dt[j][0])) print((min(ans)))
import bisect a,b,q = list(map(int,input().split())) s = [-float('inf')] + [int(eval(input())) for _ in range(a)] + [float('inf')] t = [-float('inf')] + [int(eval(input())) for _ in range(b)] + [float('inf')] for i in range(q): x = int(eval(input())) xs = bisect.bisect(s,x) xt = bisect.bisect(t,x) S = [s[xs-1],s[xs]] T = [t[xt-1],t[xt]] ans = float('inf') for j in range(2): for k in range(2): ans = min(ans, abs(S[j]-x) + abs(S[j]-T[k])) ans = min(ans, abs(T[j]-x) + abs(T[j]-S[k])) print(ans)
p03112
A, B, Q = list(map(int, input().split())) # もし神社(寺)が1つしかなかった場合に両端に仮想の神社を建築して処理の一貫性を保つ。かりにこれが選ばれても最大のコストなので最終的に最適になることはない。 INF = 10 ** 18 S = [-INF] + [int(eval(input())) for _ in range(A)]+[INF] T = [-INF] + [int(eval(input())) for _ in range(B)]+[INF] from bisect import bisect_left as bisect_right from itertools import product for q in range(Q): # 各問に答える x = int(eval(input())) # 位置の指定 s_idx, t_idx = bisect_right(S, x), bisect_right(T, x) ans = INF # 左右の神社と寺のの組み合わせ for s, t in product(S[s_idx - 1:s_idx + 1], T[t_idx - 1:t_idx + 1]): t_root = abs(t - x) + abs(s - t) # 寺から最初に回るルート s_root = abs(s - x) + abs(s - t) ans = min(ans, t_root, s_root) # 最小をとって終わり print(ans) ''' bisect_right ソートされた順序を保ったまま x を a に挿入できる点を探し当てます。リストの中から検索する部分集合を指定するには、パラメータの lo と hi を使います。デフォルトでは、リスト全体が使われます。x がすでに a に含まれている場合、挿入点は既存のどのエントリーよりも後(右)になります。戻り値は、list.insert() の第一引数として使うのに適しています。a はすでにソートされているものとします。 In[3]: A Out[3]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] In[4]: bisect.bisect_right(A, 2.5) Out[4]: 3 In[5]: bisect.bisect_right(A, 3) Out[5]: 4 '''
from sys import stdin A, B, Q = list(map(int, input().split())) # もし神社(寺)が1つしかなかった場合に両端に仮想の神社を建築して処理の一貫性を保つ。かりにこれが選ばれても最大のコストなので最終的に最適になることはない。 INF = 10 ** 18 S = [-INF] + [int(stdin.readline()) for _ in range(A)]+[INF] T = [-INF] + [int(stdin.readline()) for _ in range(B)]+[INF] from bisect import bisect_right # from bisect import bisect_left as bisect_right #これでも通ったりする。 from itertools import product for q in range(Q): # 各問に答える x = int(eval(input())) # 位置の指定 s_idx, t_idx = bisect_right(S, x), bisect_right(T, x) ans = INF # 左右の神社と寺のの組み合わせ for s, t in product(S[s_idx - 1:s_idx + 1], T[t_idx - 1:t_idx + 1]): t_root = abs(t - x) + abs(s - t) # 寺から最初に回るルート s_root = abs(s - x) + abs(s - t) ans = min(ans, t_root, s_root) # 最小をとって終わり print(ans) ''' bisect_right ソートされた順序を保ったまま x を a に挿入できる点を探し当てます。リストの中から検索する部分集合を指定するには、パラメータの lo と hi を使います。デフォルトでは、リスト全体が使われます。x がすでに a に含まれている場合、挿入点は既存のどのエントリーよりも後(右)になります。戻り値は、list.insert() の第一引数として使うのに適しています。a はすでにソートされているものとします。 In[3]: A Out[3]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] In[4]: bisect.bisect_right(A, 2.5) Out[4]: 3 In[5]: bisect.bisect_right(A, 3) Out[5]: 4 '''
p03112
from sys import stdin A, B, Q = list(map(int, input().split())) # もし神社(寺)が1つしかなかった場合に両端に仮想の神社を建築して処理の一貫性を保つ。かりにこれが選ばれても最大のコストなので最終的に最適になることはない。 INF = 10 ** 18 S = [-INF] + [int(stdin.readline()) for _ in range(A)]+[INF] T = [-INF] + [int(stdin.readline()) for _ in range(B)]+[INF] from bisect import bisect_right # from bisect import bisect_left as bisect_right #これでも通ったりする。 from itertools import product for q in range(Q): # 各問に答える x = int(eval(input())) # 位置の指定 s_idx, t_idx = bisect_right(S, x), bisect_right(T, x) ans = INF # 左右の神社と寺のの組み合わせ for s, t in product(S[s_idx - 1:s_idx + 1], T[t_idx - 1:t_idx + 1]): t_root = abs(t - x) + abs(s - t) # 寺から最初に回るルート s_root = abs(s - x) + abs(s - t) ans = min(ans, t_root, s_root) # 最小をとって終わり print(ans) ''' bisect_right ソートされた順序を保ったまま x を a に挿入できる点を探し当てます。リストの中から検索する部分集合を指定するには、パラメータの lo と hi を使います。デフォルトでは、リスト全体が使われます。x がすでに a に含まれている場合、挿入点は既存のどのエントリーよりも後(右)になります。戻り値は、list.insert() の第一引数として使うのに適しています。a はすでにソートされているものとします。 In[3]: A Out[3]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] In[4]: bisect.bisect_right(A, 2.5) Out[4]: 3 In[5]: bisect.bisect_right(A, 3) Out[5]: 4 '''
from sys import stdin A, B, Q = list(map(int, input().split())) # もし神社(寺)が1つしかなかった場合に両端に仮想の神社を建築して処理の一貫性を保つ。かりにこれが選ばれても最大のコストなので最終的に最適になることはない。 INF = 10 ** 18 S = [-INF] + list(map(int, (stdin.readline() for _ in range(A))))+[INF] T = [-INF] + list(map(int, (stdin.readline() for _ in range(B))))+[INF] from bisect import bisect_right # from bisect import bisect_left as bisect_right #これでも通ったりする。 from itertools import product for q in range(Q): # 各問に答える x = int(eval(input())) # 位置の指定 s_idx, t_idx = bisect_right(S, x), bisect_right(T, x) ans = INF # 左右の神社と寺のの組み合わせ for s, t in product(S[s_idx - 1:s_idx + 1], T[t_idx - 1:t_idx + 1]): t_root = abs(t - x) + abs(s - t) # 寺から最初に回るルート s_root = abs(s - x) + abs(s - t) ans = min(ans, t_root, s_root) # 最小をとって終わり print(ans) ''' bisect_right ソートされた順序を保ったまま x を a に挿入できる点を探し当てます。リストの中から検索する部分集合を指定するには、パラメータの lo と hi を使います。デフォルトでは、リスト全体が使われます。x がすでに a に含まれている場合、挿入点は既存のどのエントリーよりも後(右)になります。戻り値は、list.insert() の第一引数として使うのに適しています。a はすでにソートされているものとします。 In[3]: A Out[3]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] In[4]: bisect.bisect_right(A, 2.5) Out[4]: 3 In[5]: bisect.bisect_right(A, 3) Out[5]: 4 '''
p03112
# from sys import stdin #入力の高速化(0.2秒ぐらい早くなる) A, B, Q = list(map(int, input().split())) # もし神社(寺)が1つしかなかった場合に両端に仮想の神社を建築して処理の一貫性を保つ。かりにこれが選ばれても最大のコストなので最終的に最適になることはない。 INF = 10 ** 18 # S = [-INF] + list(map(int, (stdin.readline() for _ in [0] * A))) + [INF] # T = [-INF] + list(map(int, [stdin.readline() for _ in [0] * B])) + [INF] S = [-INF] + [int(eval(input())) for _ in range(A)]+[INF] T = [-INF] + [int(eval(input())) for _ in range(B)]+[INF] from bisect import bisect_right # from bisect import bisect_left as bisect_right #これでも通ったりする。 from itertools import product for q in range(Q): # 各問に答える x = int(eval(input())) # 位置の指定 s_idx, t_idx = bisect_right(S, x), bisect_right(T, x) ans = INF # 左右の神社と寺のの組み合わせ for s, t in product(S[s_idx - 1:s_idx + 1], T[t_idx - 1:t_idx + 1]): t_root = abs(t - x) + abs(s - t) # 寺から最初に回るルート s_root = abs(s - x) + abs(s - t) ans = min(ans, t_root, s_root) # 最小をとって終わり print(ans) ''' bisect_right ソートされた順序を保ったまま x を a に挿入できる点を探し当てます。リストの中から検索する部分集合を指定するには、パラメータの lo と hi を使います。デフォルトでは、リスト全体が使われます。x がすでに a に含まれている場合、挿入点は既存のどのエントリーよりも後(右)になります。戻り値は、list.insert() の第一引数として使うのに適しています。a はすでにソートされているものとします。 In[3]: A Out[3]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] In[4]: bisect.bisect_right(A, 2.5) Out[4]: 3 In[5]: bisect.bisect_right(A, 3) Out[5]: 4 '''
import bisect A, B, Q = list(map(int, input().split())) INF = 10 ** 18 s = [-INF] + [int(eval(input())) for i in range(A)] + [INF] t = [-INF] + [int(eval(input())) for i in range(B)] + [INF] for q in range(Q): x = int(eval(input())) b, d = bisect.bisect_right(s, x), bisect.bisect_right(t, x) res = INF for S in [s[b - 1], s[b]]: for T in [t[d - 1], t[d]]: d1, d2 = abs(S - x) + abs(T - S), abs(T - x) + abs(S - T) res = min(res, d1, d2) print(res)
p03112
A,B,Q = list(map(int,input().split())) Ss = [] for i in range(A): Ss.append(int(eval(input()))) Ts = [] for i in range(B): Ts.append(int(eval(input()))) def binary_search(search_list,target): left = 0 right = len(search_list) while left < right: center = (left + right)//2 if search_list[center] < target: left = center + 1 elif search_list[center] > target: right = center index = left return index-1 S_to_T = [] for i in range(A): index = binary_search(Ts,Ss[i]) if index+1 == B: right = float('inf') else: right = abs(Ss[i]-Ts[index+1]) length = min(abs(Ss[i]-Ts[index]),right) S_to_T.append(length) T_to_S = [] for i in range(B): index = binary_search(Ss,Ts[i]) if index+1 == A: right = float('inf') else: right = abs(Ts[i]-Ss[index+1]) length = min(abs(Ts[i]-Ss[index]),right) T_to_S.append(length) for i in range(Q): q = int(eval(input())) Sindex = binary_search(Ss,q) S_left = abs(Ss[Sindex]-q)+S_to_T[Sindex] if Sindex+1 == A: S_right = float('inf') else: S_right = abs(Ss[Sindex+1]-q)+S_to_T[Sindex+1] Tindex = binary_search(Ts,q) T_left = abs(Ts[Tindex]-q)+T_to_S[Tindex] if Tindex+1 == B: T_right = float('inf') else: T_right = abs(Ts[Tindex+1]-q)+T_to_S[Tindex+1] ans = min(S_left,S_right,T_left,T_right) print(ans)
import bisect A, B, Q = list(map(int, input().split())) INF = 10 ** 18 s = [-INF] + [int(eval(input())) for i in range(A)] + [INF] t = [-INF] + [int(eval(input())) for i in range(B)] + [INF] for q in range(Q): x = int(eval(input())) b, d = bisect.bisect_right(s, x), bisect.bisect_right(t, x) res = INF for S in [s[b - 1], s[b]]: for T in [t[d - 1], t[d]]: d1, d2 = abs(S - x) + abs(T - S), abs(T - x) + abs(S - T) res = min(res, d1, d2) print(res)
p03112
a, b, q = list(map(int, input().split())) S = [int(eval(input())) for _ in range(a)] T = [int(eval(input())) for _ in range(b)] S.sort() T.sort() INF = 10**18 S = [-INF] + S + [INF] T = [-INF] + T + [INF] import bisect for _ in range(q): x = int(eval(input())) i = bisect.bisect_right(S, x) j = bisect.bisect_right(T, x) ans = INF for s in [S[i-1], S[i]]: for t in [T[j-1], T[j]]: d1 = abs(s-x) + abs(t-s) d2 = abs(t-x) + abs(s-t) ans = min([ans, d1, d2]) print(ans)
import sys input = sys.stdin.buffer.readline a, b, q = list(map(int, input().split())) S = [0]*a T = [0]*b for i in range(a): s = int(eval(input())) S[i] = s for i in range(b): t = int(eval(input())) T[i] = t S.sort() T.sort() S = [-10**18]+S+[10**18] T = [-10**18]+T+[10**18] import bisect for i in range(q): x = int(eval(input())) ans = 10**18 ids = bisect.bisect_right(S, x) idt = bisect.bisect_right(T, x) for ks in (-1, 0): for kt in (-1, 0): s = S[ids+ks] t = T[idt+kt] temp = min(abs(s-x)+abs(s-t), abs(t-x)+abs(s-t)) ans = min(ans, temp) print(ans)
p03112
import bisect from functools import reduce A, B, Q = list(map(int,input().split())) INF = float("inf") s = [-1*INF] t = [-1*INF] for _ in range(A): s.append(int(eval(input()))) s.append(INF) for _ in range(B): t.append(int(eval(input()))) t.append(INF) for _ in range(Q): x = int(eval(input())) s_index = bisect.bisect_left(s,x) t_index = bisect.bisect_left(t,x) s_l = x - s[s_index-1] s_r = s[s_index] - x t_l = x - t[t_index-1] t_r = t[t_index] - x ans = reduce(min, [max(s_l,t_l), s_l + t_r*2, s_r + t_l*2, s_l*2 + t_r, s_r*2 + t_l, max(s_r,t_r)]) print(ans)
from bisect import bisect_left A, B, Q = list(map(int,input().split())) INF = 2 * 10 ** 10 S = [-INF] T = [-INF] for _ in range(A): S.append(int(eval(input()))) S.append(INF) for _ in range(B): T.append(int(eval(input()))) T.append(INF) for _ in range(Q): x = int(eval(input())) s = bisect_left(S, x) t = bisect_left(T, x) ans = min(max(S[s], T[t]) - x, x - min(S[s-1], T[t-1]), 2 * S[s] - T[t-1] - x, 2 * T[t] - S[s-1] - x, T[t] + x - 2 * S[s-1], S[s] + x - 2 * T[t-1]) print(ans)
p03112
import sys import bisect a, b, q = [int(i) for i in sys.stdin.readline().split()] s_ls = [] t_ls = [] INF = 10 ** 18 for i in range(a): s = int(eval(input())) s_ls.append(s) for i in range(b): t = int(eval(input())) t_ls.append(t) for i in range(q): x = int(eval(input())) s_ind = min(bisect.bisect_left(s_ls, x), a-1) t_ind = min(bisect.bisect_left(t_ls, x), b-1) best = INF for _s in [-1, 0]: if s_ind == 0 and _s == -1: continue for _t in [-1, 0]: if t_ind == 0 and _t == -1: continue s = s_ls[s_ind + _s] t = t_ls[t_ind + _t] if (s - x) * (t - x) >= 0: dis = max(abs(t - x), abs(s - x)) else: dis = min(abs(t - x), abs(s - x)) * 2 + max(abs(t - x), abs(s - x)) best = min(dis, best) print(best)
import sys import bisect _input = sys.stdin.readlines() a, b, q = [int(i) for i in _input[0].split()] data = [int(i) for i in _input[1:]] s_ls = data[:a] t_ls = data[a:a+b] x_ls = data[-q:] INF = 10 ** 18 for x in x_ls: s_ind = min(bisect.bisect_left(s_ls, x), a-1) t_ind = min(bisect.bisect_left(t_ls, x), b-1) best = INF for _s in [-1, 0]: if s_ind == 0 and _s == -1: continue for _t in [-1, 0]: if t_ind == 0 and _t == -1: continue s = s_ls[s_ind + _s] t = t_ls[t_ind + _t] if (s - x) * (t - x) >= 0: dis = max(abs(t - x), abs(s - x)) else: dis = min(abs(t - x), abs(s - x)) * 2 + max(abs(t - x), abs(s - x)) best = min(dis, best) print(best)
p03112
import sys input = sys.stdin.readline A,B,Q = list(map(int,input().split())) S = [] for i in range(A): temp = int(eval(input())) S.append(temp) #S.sort() T = [] for i in range(B): temp = int(eval(input())) T.append(temp) #T.sort() #print(S,T) def jinja(x): #移動距離、神社の位置を出力 if x <= S[0]: ret = [S[0]-x,0] elif x >= S[-1]: ret = [x-S[-1],A-1] else: ok = 0 ng = A while abs(ok-ng) > 1: mid = (ok+ng)//2 if S[mid] <= x: ok = mid else: ng = mid ret = [x - S[ok],ok,S[ng] - x,ng] return ret def tera(x): #移動距離、神社の位置を出力 if x <= T[0]: ret = [T[0]-x,0] elif x >= T[-1]: ret = [x-T[-1],B-1] else: ok = 0 ng = B while abs(ok-ng) > 1: mid = (ok+ng)//2 if T[mid] <= x: ok = mid else: ng = mid ret = [x - T[ok],ok,T[ng] - x,ng] return ret #P = tera(100) #print(P) for i in range(Q): INPUT = int(eval(input())) j = jinja(INPUT) #print(j) if len(j) == 2: x = j[0]; loc = S[j[1]] jt = tera(loc) if len(jt) == 2: x += jt[0] else: x += min(jt[0],jt[2]) else: x1 = j[0]; loc1 = S[j[1]]; x2 = j[2]; loc2 = S[j[3]] jt1 = tera(loc1); jt2 = tera(loc2) if len(jt1) == 2: x1 += jt1[0] else: x1 += min(jt1[0],jt1[2]) if len(jt2) == 2: x2 += jt2[0] else: x2 += min(jt2[0],jt2[2]) x = min(x1, x2) t = tera(INPUT) #print(t) if len(t) == 2: y = t[0]; loc = T[t[1]] tj = jinja(loc) #print(y,loc,tj) if len(tj) == 2: y += tj[0] else: y += min(tj[0],tj[2]) else: y1 = t[0]; loc1 = T[t[1]]; y2 = t[2]; loc2 = T[t[3]] tj1 = jinja(loc1); tj2 = jinja(loc2) if len(tj1) == 2: y1 += tj1[0] else: y1 += min(tj1[0],tj1[2]) if len(tj2) == 2: y2 += tj2[0] else: y2 += min(tj2[0],tj2[2]) y = min(y1, y2) #print(x,y) print((min(x,y)))
import bisect A, B, Q = list(map(int, input().split())) INF = 10 ** 18 s = [-INF] + [int(eval(input())) for i in range(A)] + [INF] t = [-INF] + [int(eval(input())) for i in range(B)] + [INF] for q in range(Q): x = int(eval(input())) b, d = bisect.bisect_left(s, x), bisect.bisect_left(t, x) #print(b,d) res = INF for S in [s[b - 1], s[b]]: for T in [t[d - 1], t[d]]: d1, d2 = abs(S - x) + abs(T - S), abs(T - x) + abs(S - T) res = min(res, d1, d2) print(res)
p03112
#!/usr/bin/env python3 import sys def solve(n: int, m: int, Q: int, s: "List[int]", t: "List[int]", x: "List[int]"): def check_pos(target, a, x): ret = {} a_x = a + x a_x.sort() idx = 0 while idx < len(a_x) and a_x[idx] < target[0]: dis = target[0] - a_x[idx] ret[a_x[idx]] = [dis, [target[0], dis]] # [pos, dis] idx += 1 for i in range(len(target) - 1): while idx < len(a_x) and target[i] <= a_x[idx] <= target[i + 1]: pos = a_x[idx] l = a_x[idx] - target[i] r = target[i + 1] - a_x[idx] ret[pos] = [ min(l, r), [target[i], l], [target[i + 1], r] ] idx += 1 while idx < len(a_x): dis = a_x[idx] - target[-1] ret[a_x[idx]] = [dis, [target[-1], dis]] # [pos, dis] idx += 1 return ret nearest_s = check_pos(s, t, x) nearest_t = check_pos(t, s, x) for q in x: ret = min( min([(pos_dis[1] + nearest_t[pos_dis[0]][0]) for pos_dis in nearest_s[q][1:]]), min([(pos_dis[1] + nearest_s[pos_dis[0]][0]) for pos_dis in nearest_t[q][1:]])) print(ret) return def main(): def iterate_tokens(): for line in sys.stdin: for word in line.split(): yield word tokens = iterate_tokens() A = int(next(tokens)) # type: int B = int(next(tokens)) # type: int Q = int(next(tokens)) # type: int s = [ int(next(tokens)) for _ in range(A) ] # type: "List[int]" t = [ int(next(tokens)) for _ in range(B) ] # type: "List[int]" x = [ int(next(tokens)) for _ in range(Q) ] # type: "List[int]" solve(A, B, Q, s, t, x) if __name__ == '__main__': main()
#!/usr/bin/env python3 import sys def solve(N: int, M: int, Q: int, s: "List[int]", t: "List[int]", x: "List[int]"): def binary_search(ary, val): n = len(ary) l = -1 r = n while r - l > 1: mid = (l + r) // 2 if ary[mid] >= val: r = mid else: l = mid if l == -1: return [ary[0]] if r == n: return [ary[n - 1]] return [ary[l], ary[r]] for q in x: ret = float('inf') a_pos = binary_search(s, q) b_pos = binary_search(t, q) for a in a_pos: for b in b_pos: ret = min(ret, abs(a - q) + abs(b - a)) ret = min(ret, abs(b - q) + abs(a - b)) print(ret) return def main(): def iterate_tokens(): for line in sys.stdin: for word in line.split(): yield word tokens = iterate_tokens() A = int(next(tokens)) # type: int B = int(next(tokens)) # type: int Q = int(next(tokens)) # type: int s = [ int(next(tokens)) for _ in range(A) ] # type: "List[int]" t = [ int(next(tokens)) for _ in range(B) ] # type: "List[int]" x = [ int(next(tokens)) for _ in range(Q) ] # type: "List[int]" solve(A, B, Q, s, t, x) if __name__ == '__main__': main()
p03112
import bisect A, B, Q = map(int, input().split()) S = [int(input()) for _ in range(A)] T = [int(input()) for _ in range(B)] X = [int(input()) for _ in range(Q)] R = [] for x in X: SL, SR, TL, TR = 10**20, 10**20, 10**20, 10**20 i = bisect.bisect_left(S, x) if i != 0: SL = x-S[i-1] if i != A: SR = S[i]-x j = bisect.bisect_left(T, x) if j != 0: TL = x-T[j-1] if j != B: TR = T[j]-x ans = min(max(SL, TL), min(2*SL+TR, SL+2*TR), min(2*TL+SR, TL+2*SR), max(SR, TR)) R.append(ans) print(*R, sep='\n')
import bisect A, B, Q = list(map(int, input().split())) S = [int(eval(input())) for _ in range(A)] T = [int(eval(input())) for _ in range(B)] X = [int(eval(input())) for _ in range(Q)] for x in X: SL, SR, TL, TR = 10**20, 10**20, 10**20, 10**20 i = bisect.bisect_left(S, x) if i != 0: SL = x-S[i-1] if i != A: SR = S[i]-x j = bisect.bisect_left(T, x) if j != 0: TL = x-T[j-1] if j != B: TR = T[j]-x ans = min(max(SL, TL), min(2*SL+TR, SL+2*TR), min(2*TL+SR, TL+2*SR), max(SR, TR)) print(ans)
p03112
a,b,q=list(map(int,input().split(' '))) s=[] t=[] for i in range(a): s.append(int(eval(input()))) for i in range(b): t.append(int(eval(input()))) s.sort() t.sort() for i in range(q): x=int(eval(input())) n=x start=0 end=a-1 count=0 mid=start+(end-start)//2 while start<end: if s[mid]==x: break elif s[mid]>x: end=mid mid=start+(end-start)//2 else: start=mid+1 mid=start+(end-start)//2 if s[mid]==x: count=0 elif s[mid]>x: if mid>0: count+=min(s[mid]-x,x-s[mid-1]) if s[mid]-x>x-s[mid-1]: x=s[mid-1] else: x=s[mid] else: count+=s[mid]-x x=s[mid] else: if mid<a-1: count+=min(x-s[mid],s[mid+1]-x) if x-s[mid]>s[mid+1]-x: x=s[mid+1] else: x=s[mid] else: count+=x-s[mid] x=s[mid] #print(count,x) start=0 end=b-1 mid=start+(end-start)//2 while start<end: if t[mid]==x: break elif t[mid]>x: end=mid mid=start+(end-start)//2 else: start=mid+1 mid=start+(end-start)//2 # print(mid) if t[mid]==x: pass elif t[mid]>x : if mid>0: count+=min(t[mid]-x,x-t[mid-1]) else: count+=t[mid]-x else: if mid<b-1: count+=min(x-t[mid],t[mid+1]-x) else: count+=x-t[mid] start=0 end=b-1 count1=0 x=n mid=start+(end-start)//2 while start<end: if t[mid]==x: break elif t[mid]>x: end=mid mid=start+(end-start)//2 else: start=mid+1 mid=start+(end-start)//2 if t[mid]==x: count1=0 elif t[mid]>x: if mid>0: count1+=min(t[mid]-x,x-t[mid-1]) if t[mid]-x>x-t[mid-1]: x=t[mid-1] else: x=t[mid] else: count1+=t[mid]-x x=t[mid] else: if mid<b-1: count1+=min(x-t[mid],t[mid+1]-x) if x-t[mid]>t[mid+1]-x: x=t[mid+1] else: x=t[mid] else: count1+=x-t[mid] x=t[mid] #print(count1,x) start=0 end=a-1 mid=start+(end-start)//2 while start<end: if s[mid]==x: break elif s[mid]>x: end=mid mid=start+(end-start)//2 else: start=mid+1 mid=start+(end-start)//2 if s[mid]==x: pass elif s[mid]>x : if mid>0: count1+=min(s[mid]-x,x-s[mid-1]) else: count1+=s[mid]-x else: if mid<a-1: count1+=min(x-s[mid],s[mid+1]-x) else: count1+=x-s[mid] print((min(count,count1)))
import bisect a,b,q=list(map(int,input().split(' '))) INF=10**19 s=[-INF]+[int(eval(input())) for i in range(a)]+[INF] t=[-INF]+[int(eval(input())) for i in range(b)]+[INF] s.sort() t.sort() for i in range(q): x=int(eval(input())) b,d=bisect.bisect(s,x),bisect.bisect(t,x) mi=INF for j in [s[b-1],s[b]]: for k in [t[d-1],t[d]]: mi=min(mi,abs(j-x)+abs(j-k),abs(k-x)+abs(j-k)) print(mi)
p03112
import bisect a,b,q = list(map(int,input().split())) s = [int(eval(input())) for i in range(a)] t = [int(eval(input())) for i in range(b)] #x = [int(input()) for i in range(q)] ans = 10**100 for i in range(q): x = int(eval(input())) ans = 10**100 s_idx = bisect.bisect_left(s,x) t_idx = bisect.bisect_left(t,x) s_idx -= 1 t_idx -= 1 #print(x[i],s_idx,t_idx) for j in range(2): for k in range(2): ans = min(ans,abs(x - s[min(s_idx+j,a-1)]) + abs(s[min(s_idx+j,a-1)] - t[min(t_idx+k,b-1)]),abs(x - t[min(t_idx+k,b-1)]) + abs(s[min(s_idx+j,a-1)] - t[min(t_idx+k,b-1)])) print(ans)
import sys import bisect input = sys.stdin.readline a,b,q = list(map(int,input().split())) s = [int(eval(input())) for i in range(a)] t = [int(eval(input())) for i in range(b)] #x = [int(input()) for i in range(q)] ans = 10**100 for i in range(q): x = int(eval(input())) ans = 10**100 s_idx = bisect.bisect_left(s,x) t_idx = bisect.bisect_left(t,x) s_idx -= 1 t_idx -= 1 #print(x[i],s_idx,t_idx) for j in range(2): for k in range(2): ans = min(ans,abs(x - s[min(s_idx+j,a-1)]) + abs(s[min(s_idx+j,a-1)] - t[min(t_idx+k,b-1)]),abs(x - t[min(t_idx+k,b-1)]) + abs(s[min(s_idx+j,a-1)] - t[min(t_idx+k,b-1)])) print(ans)
p03112
import sys import bisect input = sys.stdin.readline a,b,q = list(map(int,input().split())) s = [int(eval(input())) for i in range(a)] t = [int(eval(input())) for i in range(b)] #x = [int(input()) for i in range(q)] ans = 10**100 for i in range(q): x = int(eval(input())) ans = 10**100 s_idx = bisect.bisect_left(s,x) t_idx = bisect.bisect_left(t,x) s_idx -= 1 t_idx -= 1 #print(x[i],s_idx,t_idx) for j in range(2): for k in range(2): ans = min(ans,abs(x - s[min(s_idx+j,a-1)]) + abs(s[min(s_idx+j,a-1)] - t[min(t_idx+k,b-1)]),abs(x - t[min(t_idx+k,b-1)]) + abs(s[min(s_idx+j,a-1)] - t[min(t_idx+k,b-1)])) print(ans)
import sys import bisect input = sys.stdin.readline a,b,q = list(map(int,input().split())) ans = 10**100 s = [-ans] + [int(eval(input())) for i in range(a)] + [ans] t = [-ans] + [int(eval(input())) for i in range(b)] + [ans] x = [int(eval(input())) for i in range(q)] ans = 10**100 for i in range(q): ans = 10**100 s_idx = bisect.bisect_left(s,x[i]) t_idx = bisect.bisect_left(t,x[i]) s_idx -= 1 t_idx -= 1 #print(x[i],s_idx,t_idx) for j in range(2): for k in range(2): ans = min(ans,abs(x[i] - s[s_idx+j]) + abs(s[s_idx+j] - t[t_idx+k]),abs(x[i] - t[t_idx+k]) + abs(s[s_idx+j] - t[t_idx+k])) print(ans)
p03112
import sys import bisect input = sys.stdin.readline a,b,q = list(map(int,input().split())) ans = 10**100 s = [-ans] + [int(eval(input())) for i in range(a)] + [ans] t = [-ans] + [int(eval(input())) for i in range(b)] + [ans] x = [int(eval(input())) for i in range(q)] ans = 10**100 for i in range(q): ans = 10**100 s_idx = bisect.bisect_left(s,x[i]) t_idx = bisect.bisect_left(t,x[i]) s_idx -= 1 t_idx -= 1 #print(x[i],s_idx,t_idx) for j in range(2): for k in range(2): ans = min(ans,abs(x[i] - s[s_idx+j]) + abs(s[s_idx+j] - t[t_idx+k]),abs(x[i] - t[t_idx+k]) + abs(s[s_idx+j] - t[t_idx+k])) print(ans)
import sys import bisect input = sys.stdin.readline a,b,q = list(map(int,input().split())) ans = 10**100 s = [-ans] + [int(eval(input())) for i in range(a)] + [ans] t = [-ans] + [int(eval(input())) for i in range(b)] + [ans] x = [int(eval(input())) for i in range(q)] ans = 10**100 for i in range(q): ans = 10**100 s_idx = bisect.bisect_left(s,x[i]) t_idx = bisect.bisect_left(t,x[i]) s_idx -= 1 t_idx -= 1 #print(x[i],s_idx,t_idx) for j in s[s_idx+0],s[s_idx+1]: for k in t[t_idx+0],t[t_idx+1]: ans = min(ans,abs(x[i] - j) + abs(j - k),abs(x[i] - k) + abs(j - k)) print(ans)
p03112
import sys import bisect input = sys.stdin.readline a,b,q = list(map(int,input().split())) ans = 10**100 s = [-ans] + [int(eval(input())) for i in range(a)] + [ans] t = [-ans] + [int(eval(input())) for i in range(b)] + [ans] x = [int(eval(input())) for i in range(q)] ans = 10**100 for i in range(q): ans = 10**100 s_idx = bisect.bisect_left(s,x[i]) t_idx = bisect.bisect_left(t,x[i]) s_idx -= 1 t_idx -= 1 #print(x[i],s_idx,t_idx) for j in s[s_idx+0],s[s_idx+1]: for k in t[t_idx+0],t[t_idx+1]: ans = min(ans,abs(x[i] - j) + abs(j - k),abs(x[i] - k) + abs(j - k)) print(ans)
import sys import bisect input = sys.stdin.readline a,b,q = list(map(int,input().split())) ans = 10**100 s = [-ans] + [int(eval(input())) for i in range(a)] + [ans] t = [-ans] + [int(eval(input())) for i in range(b)] + [ans] #x = [int(input()) for i in range(q)] ans = 10**100 for i in range(q): x = int(eval(input())) ans = 10**100 s_idx = bisect.bisect_left(s,x) t_idx = bisect.bisect_left(t,x) s_idx -= 1 t_idx -= 1 #print(x[i],s_idx,t_idx) for j in s[s_idx+0],s[s_idx+1]: for k in t[t_idx+0],t[t_idx+1]: ans = min(ans,abs(x - j) + abs(j - k),abs(x - k) + abs(j - k)) print(ans)
p03112
from bisect import bisect_left, bisect_right a, b, q = list(map(int, input().split())) s = [int(eval(input())) for _ in range(a)] t = [int(eval(input())) for _ in range(b)] q = [int(eval(input())) for _ in range(q)] for qi in q: qans = 10**18 ai = bisect_left(s, qi) bi = bisect_left(t, qi) ac = [] bc = [] for i in range(3): if (0 <= ai + i < a): ac.append(ai + i); if (0 <= ai - i < a): ac.append(ai - i); for i in range(3): if (0 <= bi + i < b): bc.append(bi + i); if (0 <= bi - i < b): bc.append(bi - i); for i in ac: for j in bc: aa = s[i]; bb = t[j]; if (max(aa, bb) <= qi): qans = min(qans, qi - min(aa, bb)) elif (qi <= min(aa, bb)): qans = min(qans, max(aa, bb) - qi) else: qans = min(qans, min(abs(aa - qi), abs(bb - qi)) * 2 + max(abs(aa - qi), abs(bb - qi))) print(qans)
from bisect import bisect_left, bisect_right a, b, q = list(map(int, input().split())) s = [int(eval(input())) for _ in range(a)] t = [int(eval(input())) for _ in range(b)] q = [int(eval(input())) for _ in range(q)] for qi in q: qans = 10**18 ai = bisect_left(s, qi) bi = bisect_left(t, qi) ac = [] bc = [] for i in range(2): if (0 <= ai + i < a): ac.append(ai + i); if (0 <= ai - i < a): ac.append(ai - i); for i in range(2): if (0 <= bi + i < b): bc.append(bi + i); if (0 <= bi - i < b): bc.append(bi - i); for i in ac: for j in bc: aa = s[i]; bb = t[j]; if (max(aa, bb) <= qi): qans = min(qans, qi - min(aa, bb)) elif (qi <= min(aa, bb)): qans = min(qans, max(aa, bb) - qi) else: qans = min(qans, min(abs(aa - qi), abs(bb - qi)) * 2 + max(abs(aa - qi), abs(bb - qi))) print(qans)
p03112
from bisect import bisect_left, bisect_right a, b, q = list(map(int, input().split())) s = [int(eval(input())) for _ in range(a)] t = [int(eval(input())) for _ in range(b)] q = [int(eval(input())) for _ in range(q)] for qi in q: qans = 10**18 ai = bisect_left(s, qi) bi = bisect_left(t, qi) ac = [] bc = [] for i in range(2): if (0 <= ai + i < a): ac.append(ai + i); if (0 <= ai - i < a): ac.append(ai - i); for i in range(2): if (0 <= bi + i < b): bc.append(bi + i); if (0 <= bi - i < b): bc.append(bi - i); for i in ac: for j in bc: aa = s[i]; bb = t[j]; if (max(aa, bb) <= qi): qans = min(qans, qi - min(aa, bb)) elif (qi <= min(aa, bb)): qans = min(qans, max(aa, bb) - qi) else: qans = min(qans, min(abs(aa - qi), abs(bb - qi)) * 2 + max(abs(aa - qi), abs(bb - qi))) print(qans)
from bisect import bisect_left, bisect_right import sys input = sys.stdin.readline a, b, q = list(map(int, input().split())) s = [int(eval(input())) for _ in range(a)] t = [int(eval(input())) for _ in range(b)] q = [int(eval(input())) for _ in range(q)] for qi in q: qans = 10**18 ai = bisect_left(s, qi) bi = bisect_left(t, qi) ac = [] bc = [] for i in range(2): if (0 <= ai + i < a): ac.append(ai + i); if (0 <= ai - i < a): ac.append(ai - i); for i in range(2): if (0 <= bi + i < b): bc.append(bi + i); if (0 <= bi - i < b): bc.append(bi - i); for i in ac: for j in bc: aa = s[i]; bb = t[j]; if (max(aa, bb) <= qi): qans = min(qans, qi - min(aa, bb)) elif (qi <= min(aa, bb)): qans = min(qans, max(aa, bb) - qi) else: qans = min(qans, min(abs(aa - qi), abs(bb - qi)) * 2 + max(abs(aa - qi), abs(bb - qi))) print(qans)
p03112
import bisect A, B, Q = list(map(int, input().split())) S = [-float("inf")] T = [-float("inf")] X = [] for i in range(A): S.append(int(eval(input()))) S.append(float("inf")) for i in range(B): T.append(int(eval(input()))) T.append(float("inf")) for i in range(Q): X.append(int(eval(input()))) for x in X: si = bisect.bisect_left(S, x) ti = bisect.bisect_left(T, x) s01 = [S[si-1], S[si]] t01 = [T[ti-1], T[ti]] ans = float("inf") for ss in s01: for tt in t01: l = abs(ss - tt) + min(abs(ss - x), abs(tt - x)) ans = min(ans, l) print(ans)
import bisect A, B, Q = list(map(int, input().split())) S = [int(eval(input())) for i in range(A)] S.insert(0, -float("inf")) S.append(float("inf")) T = [int(eval(input())) for i in range(B)] T.insert(0, -float("inf")) T.append(float("inf")) X = [int(eval(input())) for i in range(Q)] for x in X: ans = float("inf") idx_a = bisect.bisect_right(S, x) idx_b = bisect.bisect_right(T, x) for s in [S[idx_a-1], S[idx_a]]: for t in [T[idx_b-1], T[idx_b]]: temp = abs(s-t) + min(abs(x-s), abs(x-t)) if temp < ans: ans = temp print(ans)
p03112
import bisect A,B,C =list(map(int,input().split())) s =[-10**11] + [int(eval(input()))for _ in range(A)] +[10**11] t =[-10**11] + [int(eval(input()))for _ in range(B)] +[10**11] for _ in range(C): x =int(eval(input())) ns = bisect.bisect_left(s,x) nt = bisect.bisect_left(t,x) sx =[s[ns-1],s[ns]] tx =[t[nt-1],t[nt]] lis =[sx,tx] ans = 10**11 for i in range(8): p =i%2 q =(i//2)%2 r =i//4 foo =abs(x-lis[p][q])+abs(lis[p][q]-lis[p-1][r]) if foo < ans: ans = foo print(ans)
import bisect import sys input = sys.stdin.readline A,B,C =list(map(int,input().split())) s =[-10**11] + [int(eval(input()))for _ in range(A)] +[10**11] t =[-10**11] + [int(eval(input()))for _ in range(B)] +[10**11] for _ in range(C): x =int(eval(input())) ns = bisect.bisect_left(s,x) nt = bisect.bisect_left(t,x) sx =[s[ns-1],s[ns]] tx =[t[nt-1],t[nt]] lis =[sx,tx] ans = 10**11 for i in range(8): p =i%2 q =(i//2)%2 r =i//4 foo =abs(x-lis[p][q])+abs(lis[p][q]-lis[p-1][r]) if foo < ans: ans = foo print(ans)
p03112
import bisect A, B, Q = list(map(int, input().split())) INF = 10 ** 18 s = [-INF] + [int(eval(input())) for i in range(A)] + [INF] t = [-INF] + [int(eval(input())) for i in range(B)] + [INF] for q in range(Q): x = int(eval(input())) b, d = bisect.bisect_right(s, x), bisect.bisect_right(t, x) res = INF for S in [s[b - 1], s[b]]: for T in [t[d - 1], t[d]]: d1, d2 = abs(S - x) + abs(T - S), abs(T - x) + abs(S - T) res = min(res, d1, d2) print(res)
import bisect A, B, Q = list(map(int, input().split())) INF = 10 ** 18 S = [-INF] + [int(eval(input())) for _ in range(A)] + [INF] T = [-INF] + [int(eval(input())) for _ in range(B)] + [INF] for _ in range(Q): x = int(eval(input())) s1 = bisect.bisect_left(S, x) -1 #自分の左側のindex s2 = s1 + 1 #自分の右側のindex t1 = bisect.bisect_left(T, x) -1 #自分の左側のindex t2 = t1 + 1 #自分の右側のindex res = 10 ** 18 for s in [S[s1], S[s2]]: for t in [T[t1], T[t2]]: d1, d2 = abs(s-x) + abs(t-s), abs(t-x) + abs(s-t) res = min(res, d1, d2) print (res)
p03112
import bisect a, b, q = list(map(int, input().split())) s = [int(eval(input())) for _ in range(a)] t = [int(eval(input())) for _ in range(b)] INF = float('inf') for _ in range(q): x = int(eval(input())) if x > s[a-1]: sl = x - s[a-1] sr = INF elif x < s[0]: sl = INF sr = s[0] - x else: si = bisect.bisect_right(s, x) sl = x - s[si-1] sr = s[si] - x if x > t[b-1]: tl = x - t[b-1] tr = INF elif x < t[0]: tl = INF tr = t[0] - x else: ti = bisect.bisect_right(t, x) tl = x - t[ti-1] tr = t[ti] - x ans = min(max(sl, tl), max(sr, tr)) ans = min(ans, sr*2+tl) ans = min(ans, sr+tl*2) ans = min(ans, sl*2+tr) ans = min(ans, sl+tr*2) print(ans)
import bisect import sys input = sys.stdin.readline a, b, q = list(map(int, input().split())) s = [int(eval(input())) for _ in range(a)] t = [int(eval(input())) for _ in range(b)] INF = float('inf') for _ in range(q): x = int(eval(input())) if x > s[a-1]: sl = x - s[a-1] sr = INF elif x < s[0]: sl = INF sr = s[0] - x else: si = bisect.bisect_right(s, x) sl = x - s[si-1] sr = s[si] - x if x > t[b-1]: tl = x - t[b-1] tr = INF elif x < t[0]: tl = INF tr = t[0] - x else: ti = bisect.bisect_right(t, x) tl = x - t[ti-1] tr = t[ti] - x ans = min(max(sl, tl), max(sr, tr)) ans = min(ans, sr*2+tl) ans = min(ans, sr+tl*2) ans = min(ans, sl*2+tr) ans = min(ans, sl+tr*2) print(ans)
p03112
import bisect a,b,q=list(map(int,input().split())) s=[] t=[] x=[] s.append(-float("inf")) t.append(-float("inf")) for i in range(a): s.append(int(eval(input()))) for i in range(b): t.append(int(eval(input()))) s.append(float("inf")) t.append(float("inf")) s.sort() t.sort() for i in range(q): x=int(eval(input())) ans=float("inf") tmp_s_1=bisect.bisect_left(s,x)-1 tmp_s_2=tmp_s_1+1 tmp_s_1_t=bisect.bisect_left(t,s[tmp_s_1]) tmp_s_2_t=bisect.bisect_left(t,s[tmp_s_2]) ans=min(abs(x-s[tmp_s_1])+abs(s[tmp_s_1]-t[tmp_s_1_t-1]), abs(x-s[tmp_s_1])+abs(s[tmp_s_1]-t[tmp_s_1_t]), abs(x-s[tmp_s_2])+abs(s[tmp_s_2]-t[tmp_s_2_t-1]), abs(x-s[tmp_s_2])+abs(s[tmp_s_2]-t[tmp_s_2_t])) tmp_t_1=bisect.bisect_left(t,x)-1 tmp_t_2=tmp_t_1+1 tmp_t_1_s=bisect.bisect_left(s,t[tmp_t_1]) tmp_t_2_s=bisect.bisect_left(s,t[tmp_t_2]) ans=min(ans, abs(x-t[tmp_t_1])+abs(t[tmp_t_1]-s[tmp_t_1_s-1]), abs(x-t[tmp_t_1])+abs(t[tmp_t_1]-s[tmp_t_1_s]), abs(x-t[tmp_t_2])+abs(t[tmp_t_2]-s[tmp_t_2_s-1]), abs(x-t[tmp_t_2])+abs(t[tmp_t_2]-s[tmp_t_2_s])) print(ans)
from bisect import bisect_left a,b,q=list(map(int,input().split())) s=[] t=[] x=[] s.append(-float("inf")) t.append(-float("inf")) for i in range(a): s.append(int(eval(input()))) for i in range(b): t.append(int(eval(input()))) s.append(float("inf")) t.append(float("inf")) s.sort() t.sort() for i in range(q): x=int(eval(input())) ans=float("inf") tmp_s_1=bisect_left(s,x)-1 tmp_s_2=tmp_s_1+1 tmp_s_1_t=bisect_left(t,s[tmp_s_1]) tmp_s_2_t=bisect_left(t,s[tmp_s_2]) ans=min(abs(x-s[tmp_s_1])+abs(s[tmp_s_1]-t[tmp_s_1_t-1]), abs(x-s[tmp_s_1])+abs(s[tmp_s_1]-t[tmp_s_1_t]), abs(x-s[tmp_s_2])+abs(s[tmp_s_2]-t[tmp_s_2_t-1]), abs(x-s[tmp_s_2])+abs(s[tmp_s_2]-t[tmp_s_2_t])) tmp_t_1=bisect_left(t,x)-1 tmp_t_2=tmp_t_1+1 tmp_t_1_s=bisect_left(s,t[tmp_t_1]) tmp_t_2_s=bisect_left(s,t[tmp_t_2]) ans=min(ans, abs(x-t[tmp_t_1])+abs(t[tmp_t_1]-s[tmp_t_1_s-1]), abs(x-t[tmp_t_1])+abs(t[tmp_t_1]-s[tmp_t_1_s]), abs(x-t[tmp_t_2])+abs(t[tmp_t_2]-s[tmp_t_2_s-1]), abs(x-t[tmp_t_2])+abs(t[tmp_t_2]-s[tmp_t_2_s])) print(ans)
p03112
from bisect import bisect_left a,b,q=list(map(int,input().split())) s=[] t=[] x=[] s.append(-float("inf")) t.append(-float("inf")) for i in range(a): s.append(int(eval(input()))) for i in range(b): t.append(int(eval(input()))) s.append(float("inf")) t.append(float("inf")) for i in range(q): x=int(eval(input())) ans=float("inf") tmp_s_1=bisect_left(s,x)-1 tmp_s_2=tmp_s_1+1 tmp_s_1_t=bisect_left(t,s[tmp_s_1]) tmp_s_2_t=bisect_left(t,s[tmp_s_2]) ans=min(abs(x-s[tmp_s_1])+abs(s[tmp_s_1]-t[tmp_s_1_t-1]), abs(x-s[tmp_s_1])+abs(s[tmp_s_1]-t[tmp_s_1_t]), abs(x-s[tmp_s_2])+abs(s[tmp_s_2]-t[tmp_s_2_t-1]), abs(x-s[tmp_s_2])+abs(s[tmp_s_2]-t[tmp_s_2_t])) tmp_t_1=bisect_left(t,x)-1 tmp_t_2=tmp_t_1+1 tmp_t_1_s=bisect_left(s,t[tmp_t_1]) tmp_t_2_s=bisect_left(s,t[tmp_t_2]) ans=min(ans, abs(x-t[tmp_t_1])+abs(t[tmp_t_1]-s[tmp_t_1_s-1]), abs(x-t[tmp_t_1])+abs(t[tmp_t_1]-s[tmp_t_1_s]), abs(x-t[tmp_t_2])+abs(t[tmp_t_2]-s[tmp_t_2_s-1]), abs(x-t[tmp_t_2])+abs(t[tmp_t_2]-s[tmp_t_2_s])) print(ans)
from bisect import bisect_left a,b,q=list(map(int,input().split())) s=[] t=[] x=[] s.append(-10**12) t.append(-10**12) for i in range(a): s.append(int(eval(input()))) for i in range(b): t.append(int(eval(input()))) s.append(10**12) t.append(10**12) for i in range(q): x=int(eval(input())) tmp_s=bisect_left(s,x)-1 tmp_s_1=s[tmp_s] tmp_s_2=s[tmp_s+1] tmp_t=bisect_left(t,x)-1 tmp_t_1=t[tmp_t] tmp_t_2=t[tmp_t+1] print((min(abs(x-tmp_s_1)+abs(tmp_s_1-tmp_t_1), abs(x-tmp_s_1)+abs(tmp_s_1-tmp_t_2), abs(x-tmp_s_2)+abs(tmp_s_2-tmp_t_1), abs(x-tmp_s_2)+abs(tmp_s_2-tmp_t_2), abs(x-tmp_t_1)+abs(tmp_t_1-tmp_s_1), abs(x-tmp_t_1)+abs(tmp_t_1-tmp_s_2), abs(x-tmp_t_2)+abs(tmp_t_2-tmp_s_1), abs(x-tmp_t_2)+abs(tmp_t_2-tmp_s_2))))
p03112
from bisect import bisect_left a,b,q = list(map(int,input().split())) inf = float('inf') S = [-inf]+[int(eval(input())) for i in range(a)]+[inf] T = [-inf]+[int(eval(input())) for i in range(b)]+[inf] def f(S,T): res = 10**30 s = bisect_left(S,x) ans = 0 t = bisect_left(T,S[s-1]) ans += abs(x-S[s-1])+min(abs(S[s-1]-T[t-1]),abs(S[s-1]-T[t])) if ans <= res: res = ans ans = 0 t = bisect_left(T,S[s]) ans += abs(x-S[s])+min(abs(S[s]-T[t-1]),abs(S[s]-T[t])) if ans <= res: res = ans return res for i in range(q): x = int(eval(input())) print((min(f(S,T),f(T,S))))
from bisect import bisect_left import sys input = sys.stdin.readline a,b,q = list(map(int,input().split())) inf = float('inf') S = [-inf]+[int(eval(input())) for i in range(a)]+[inf] T = [-inf]+[int(eval(input())) for i in range(b)]+[inf] def f(S,T): res = 10**30 s = bisect_left(S,x) ans = 0 t = bisect_left(T,S[s-1]) ans += abs(x-S[s-1])+min(abs(S[s-1]-T[t-1]),abs(S[s-1]-T[t])) if ans <= res: res = ans ans = 0 t = bisect_left(T,S[s]) ans += abs(x-S[s])+min(abs(S[s]-T[t-1]),abs(S[s]-T[t])) if ans <= res: res = ans return res for i in range(q): x = int(eval(input())) print((min(f(S,T),f(T,S))))
p03112
import bisect,sys input = sys.stdin.readline a,b,q = list(map(int,input().split())) INF = float('inf') S = [-INF]+[int(eval(input())) for i in range(a)]+[INF] T = [-INF]+[int(eval(input())) for i in range(b)]+[INF] def f(S,T): res = INF s = bisect.bisect_left(S,x) for i in range(2): for j in range(2): t = bisect.bisect_left(T,S[s-i]) ans= abs(x-S[s-i])+abs(S[s-i]-T[t-j]) if ans < res: res = ans return res for i in range(q): x = int(eval(input())) print((min(f(S,T),f(T,S))))
from bisect import bisect_left import sys input = sys.stdin.readline a,b,q = list(map(int,input().split())) inf = float('inf') S = [-inf]+[int(eval(input())) for i in range(a)]+[inf] T = [-inf]+[int(eval(input())) for i in range(b)]+[inf] def f(S,T): res = 10**30 s = bisect_left(S,x) ans = 0 t = bisect_left(T,S[s-1]) ans += abs(x-S[s-1])+min(abs(S[s-1]-T[t-1]),abs(S[s-1]-T[t])) if ans <= res: res = ans ans = 0 t = bisect_left(T,S[s]) ans += abs(x-S[s])+min(abs(S[s]-T[t-1]),abs(S[s]-T[t])) if ans <= res: res = ans return res for i in range(q): x = int(eval(input())) print((min(f(S,T),f(T,S))))
p03112
A, B, Q = list(map(int, input().split())) s = [int(eval(input())) for i in range(A)] t = [int(eval(input())) for i in range(B)] x = [int(eval(input())) for i in range(Q)] def search(a): cur = a sx, tx = [], [] #xiとsi、tiの差のリスト for si in s: sx.append(abs(si - a)) for ti in t: tx.append(abs(ti - a)) stmin = [[sx.index(min(sx)),min(sx)], [tx.index(min(tx)),min(tx)]] ans = min(stmin[0][1], stmin[1][1]) # if stmin[0][1] <= stmin[1][1]: #sのほうが近いとき cur1 = s[stmin[0][0]] tcur = [] for ti in t: tcur.append(abs(ti - cur1)) ans1 = stmin[0][1] + min(tcur) # if stmin[0][1] > stmin[1][1]: #tのほうが近いとき cur2 = t[stmin[1][0]] scur = [] for si in s: scur.append(abs(si - cur2)) ans2 = stmin[1][1] + min(scur) print((min(ans1, ans2))) for xi in x: search(xi)
import bisect A, B, Q = list(map(int, input().split())) INF = 10 ** 18 s = [-INF] + [int(eval(input())) for i in range(A)] + [INF] t = [-INF] + [int(eval(input())) for i in range(B)] + [INF] for q in range(Q): x = int(eval(input())) b, d = bisect.bisect_right(s, x), bisect.bisect_right(t, x) res = INF for S in [s[b - 1], s[b]]: for T in [t[d - 1], t[d]]: d1, d2 = abs(S - x) + abs(T - S), abs(T - x) + abs(S - T) res = min(res, d1, d2) print(res)
p03112
import bisect A, B, Q = list(map(int, input().split())) S = [int(eval(input())) for _ in range(A)] T = [int(eval(input())) for _ in range(B)] X = [int(eval(input())) for _ in range(Q)] #print("S:", S) #print("T:", T) for i in range(Q): x = X[i] answer = 10 ** 12 i1 = bisect.bisect_right(S, x) - 1 i2 = (i1 + 1) % A S1 = S[i1] S2 = S[i2] dist1 = abs(x - S1) dist2 = abs(x - S2) #print("dist1, dist2:", dist1, dist2) i11 = bisect.bisect_right(T, S1) - 1 i12 = (i11 + 1) % B T11 = T[i11] T12 = T[i12] dist11 = abs(S1 - T11) dist12 = abs(S1 - T12) #print("dist11, dist12:", dist11, dist12) i21 = bisect.bisect_right(T, S2) - 1 i22 = (i21 + 1) % B T21 = T[i21] T22 = T[i22] dist21 = abs(S2 - T21) dist22 = abs(S2 - T22) #print("dist21, dist22:", dist21, dist22) dist = min(dist1 + min(dist11, dist12), dist2 + min(dist21, dist22)) answer = min(answer, dist) S, T = T, S A, B = B, A i1 = bisect.bisect_right(S, x) - 1 i2 = (i1 + 1) % A S1 = S[i1] S2 = S[i2] dist1 = abs(x - S1) dist2 = abs(x - S2) #print("dist1, dist2:", dist1, dist2) i11 = bisect.bisect_right(T, S1) - 1 i12 = (i11 + 1) % B T11 = T[i11] T12 = T[i12] dist11 = abs(S1 - T11) dist12 = abs(S1 - T12) #print("dist11, dist12:", dist11, dist12) i21 = bisect.bisect_right(T, S2) - 1 i22 = (i21 + 1) % B T21 = T[i21] T22 = T[i22] dist21 = abs(S2 - T21) dist22 = abs(S2 - T22) #print("dist21, dist22:", dist21, dist22) dist = min(dist1 + min(dist11, dist12), dist2 + min(dist21, dist22)) answer = min(answer, dist) S, T = T, S A, B = B, A print(answer)
import bisect A, B, Q = list(map(int, input().split())) S = [int(eval(input())) for _ in range(A)] T = [int(eval(input())) for _ in range(B)] X = [int(eval(input())) for _ in range(Q)] #print("S:", S) #print("T:", T) answer1 = [] answer2 = [] for x in X: answer = 10 ** 12 i1 = bisect.bisect_right(S, x) - 1 i2 = (i1 + 1) % A S1 = S[i1] S2 = S[i2] dist1 = abs(x - S1) dist2 = abs(x - S2) #print("dist1, dist2:", dist1, dist2) i11 = bisect.bisect_right(T, S1) - 1 i12 = (i11 + 1) % B T11 = T[i11] T12 = T[i12] dist11 = abs(S1 - T11) dist12 = abs(S1 - T12) #print("dist11, dist12:", dist11, dist12) i21 = bisect.bisect_right(T, S2) - 1 i22 = (i21 + 1) % B T21 = T[i21] T22 = T[i22] dist21 = abs(S2 - T21) dist22 = abs(S2 - T22) #print("dist21, dist22:", dist21, dist22) dist = min(dist1 + min(dist11, dist12), dist2 + min(dist21, dist22)) answer1.append(dist) S, T = T, S A, B = B, A for x in X: answer = 10 ** 15 i1 = bisect.bisect_right(S, x) - 1 i2 = (i1 + 1) % A S1 = S[i1] S2 = S[i2] dist1 = abs(x - S1) dist2 = abs(x - S2) #print("dist1, dist2:", dist1, dist2) i11 = bisect.bisect_right(T, S1) - 1 i12 = (i11 + 1) % B T11 = T[i11] T12 = T[i12] dist11 = abs(S1 - T11) dist12 = abs(S1 - T12) #print("dist11, dist12:", dist11, dist12) i21 = bisect.bisect_right(T, S2) - 1 i22 = (i21 + 1) % B T21 = T[i21] T22 = T[i22] dist21 = abs(S2 - T21) dist22 = abs(S2 - T22) #print("dist21, dist22:", dist21, dist22) dist = min(dist1 + min(dist11, dist12), dist2 + min(dist21, dist22)) answer2.append(dist) for i in range(Q): print((min(answer1[i], answer2[i])))
p03112
import bisect def solve(A, B, Q, Ss, Ts, Xs): ans = [] Ss.append(10 ** 21) Ss.insert(0, -10 ** 21) Ts.append(10 ** 21) Ts.insert(0, -10 ** 21) for x in Xs: s_right = bisect.bisect_right(Ss, x) s_left = max(s_right - 1, 0) t_right = bisect.bisect_right(Ts, x) t_left = max(t_right - 1, 0) s_right = Ss[s_right] s_left = Ss[s_left] t_right = Ts[t_right] t_left = Ts[t_left] d_s_right = abs(s_right - x) d_s_left = abs(s_left - x) d_t_right = abs(t_right - x) d_t_left = abs(t_left - x) tans = 10 ** 35 if x <= s_right and x <= t_right: tans = min(tans, max(d_t_right, d_s_right)) if s_right <= x and t_right <= x: tans = min(tans, max(d_t_right, d_s_right)) if s_left <= x and t_left <= x: tans = min(tans, max(d_s_left, d_t_left)) tans = min(tans, min(d_s_right, d_t_left) * 2 + max(d_s_right, d_t_left)) tans = min(tans, min(d_t_right, d_s_left) * 2 + max(d_t_right, d_s_left)) ans.append(str(tans)) return "\n".join(ans) if __name__ == "__main__": A, B, Q = tuple(map(int, input().split(" "))) Ss = [int(eval(input())) for _ in range(A)] Ts = [int(eval(input())) for _ in range(B)] Xs = [int(eval(input())) for _ in range(Q)] print((solve(A, B, Q, Ss, Ts, Xs)))
import bisect def solve(A, B, Q, Ss, Ts, Xs): ans = [] Ss.append(10 ** 21) Ss.insert(0, -10 ** 21) Ts.append(10 ** 21) Ts.insert(0, -10 ** 21) for x in Xs: s_right = bisect.bisect_right(Ss, x) s_left = max(s_right - 1, 0) t_right = bisect.bisect_right(Ts, x) t_left = max(t_right - 1, 0) s_right = Ss[s_right] s_left = Ss[s_left] t_right = Ts[t_right] t_left = Ts[t_left] d_s_right = abs(s_right - x) d_s_left = abs(s_left - x) d_t_right = abs(t_right - x) d_t_left = abs(t_left - x) tans = 10 ** 35 if x <= s_right and x <= t_right: tans = min(tans, max(d_t_right, d_s_right)) if s_left <= x and t_left <= x: tans = min(tans, max(d_s_left, d_t_left)) tans = min(tans, min(d_s_right, d_t_left) * 2 + max(d_s_right, d_t_left)) tans = min(tans, min(d_t_right, d_s_left) * 2 + max(d_t_right, d_s_left)) ans.append(str(tans)) return "\n".join(ans) if __name__ == "__main__": A, B, Q = tuple(map(int, input().split(" "))) Ss = [int(eval(input())) for _ in range(A)] Ts = [int(eval(input())) for _ in range(B)] Xs = [int(eval(input())) for _ in range(Q)] print((solve(A, B, Q, Ss, Ts, Xs)))
p03112
from bisect import bisect_right def getINearests(ss, ts): iNearests = [-1] * len(ss) for iS, s in enumerate(ss): iT = bisect_right(ts, s) if iT == len(ts): iNearests[iS] = iT-1 elif iT == 0: iNearests[iS] = 0 else: d1 = abs(s - ts[iT-1]) d2 = abs(s - ts[iT]) if d1 <= d2: iNearests[iS] = iT-1 else: iNearests[iS] = iT return iNearests A, B, Q = list(map(int, input().split())) ss = [int(eval(input())) for _ in range(A)] ts = [int(eval(input())) for _ in range(B)] xs = [int(eval(input())) for _ in range(Q)] iTNearests = getINearests(ss, ts) iSNearests = getINearests(ts, ss) anss = [0] * Q for iX, x in enumerate(xs): ans = float('inf') # 初手: 神社 iS = bisect_right(ss, x) if iS > 0: cost = abs(x - ss[iS-1]) cost += abs(ss[iS-1] - ts[iTNearests[iS-1]]) ans = min(ans, cost) if iS < A: cost = abs(x - ss[iS]) cost += abs(ss[iS] - ts[iTNearests[iS]]) ans = min(ans, cost) # 初手: 寺 iT = bisect_right(ts, x) if iT > 0: cost = abs(x - ts[iT-1]) cost += abs(ts[iT-1] - ss[iSNearests[iT-1]]) ans = min(ans, cost) if iT < B: cost = abs(x - ts[iT]) cost += abs(ts[iT] - ss[iSNearests[iT]]) ans = min(ans, cost) anss[iX] = ans print(('\n'.join(map(str, anss))))
import sys from bisect import bisect_right def solve(): def getINearests(ss, ts): iNearests = [-1] * len(ss) for iS, s in enumerate(ss): iT = bisect_right(ts, s) if iT == len(ts): iNearests[iS] = iT-1 elif iT == 0: iNearests[iS] = 0 else: d1 = abs(s - ts[iT-1]) d2 = abs(s - ts[iT]) if d1 <= d2: iNearests[iS] = iT-1 else: iNearests[iS] = iT return iNearests A, B, Q = list(map(int, input().split())) ss = [int(sys.stdin.readline()) for _ in range(A)] ts = [int(sys.stdin.readline()) for _ in range(B)] xs = [int(sys.stdin.readline()) for _ in range(Q)] iTNearests = getINearests(ss, ts) iSNearests = getINearests(ts, ss) anss = [0] * Q for iX, x in enumerate(xs): ans = float('inf') # 初手: 神社 iS = bisect_right(ss, x) if iS > 0: cost = abs(x - ss[iS-1]) cost += abs(ss[iS-1] - ts[iTNearests[iS-1]]) if cost < ans: ans = cost if iS < A: cost = abs(x - ss[iS]) cost += abs(ss[iS] - ts[iTNearests[iS]]) if cost < ans: ans = cost # 初手: 寺 iT = bisect_right(ts, x) if iT > 0: cost = abs(x - ts[iT-1]) cost += abs(ts[iT-1] - ss[iSNearests[iT-1]]) if cost < ans: ans = cost if iT < B: cost = abs(x - ts[iT]) cost += abs(ts[iT] - ss[iSNearests[iT]]) if cost < ans: ans = cost anss[iX] = ans print(('\n'.join(map(str, anss)))) solve()
p03112
import bisect a, b, q = list(map(int, input().split())) INF = 10 ** 18 s = [-INF] + [int(eval(input())) for i in range(a)] + [INF] t = [-INF] + [int(eval(input())) for i in range(b)] + [INF] for question in range(q): x = int(eval(input())) b, d = bisect.bisect_right(s, x), bisect.bisect_right(t, x) res = INF for shrine in [s[b - 1], s[b]]: for temple in [t[d - 1], t[d]]: d1, d2 = abs(shrine - x) + abs(temple - shrine), abs(temple - x) + abs(shrine - temple) res = min(res, d1, d2) print(res)
from bisect import bisect_right as br a,b,q=list(map(int,input().split())) I=2<<60 s,t=[-I]+[int(eval(input())) for _ in range(a)]+[I],[-I]+[int(eval(input())) for _ in range(b)]+[I] for question in range(q): x=int(eval(input())) b,d,r=br(s,x),br(t,x),I for S in [s[b-1],s[b]]: for T in [t[d-1],t[d]]: d1,d2=abs(S-x)+abs(T-S),abs(T-x)+abs(S-T) r=min(r,d1,d2) print(r)
p03112
import bisect A, B, Q = list(map(int, input().split())) s = [-10 ** 12] + [int(eval(input())) for _ in range(A)] + [10 ** 12] t = [-10 ** 12] + [int(eval(input())) for _ in range(B)] + [10 ** 12] x = [int(eval(input())) for _ in range(Q)] for i in x: si = bisect.bisect_left(s, i) ti = bisect.bisect_left(t, i) s1, s2 = s[si - 1], s[si] t1, t2 = t[ti - 1], t[ti] ans = 10 ** 12 for ss in [s1, s2]: for tt in [t1, t2]: ans = min(ans, abs(i - ss) + abs(ss - tt), abs(i - tt) + abs(ss - tt)) print(ans)
import bisect A, B, Q = list(map(int, input().split())) S = [int(eval(input())) for _ in range(A)] T = [int(eval(input())) for _ in range(B)] X = [int(eval(input())) for _ in range(Q)] S = [-10 ** 12] + S + [10 ** 12] T = [-10 ** 12] + T + [10 ** 12] for x in X: si = bisect.bisect_right(S, x) ti = bisect.bisect_right(T, x) Ls, Rs = x - S[si - 1], -x + S[si] Lt, Rt = x - T[ti - 1], -x + T[ti] ans = min(Ls * 2 + Rt, Ls + Rt * 2, Lt * 2 + Rs, Lt + Rs * 2, max(Ls, Lt), max(Rs, Rt)) print(ans)
p03112
A, B, Q = list(map(int, input().split())) s = [int(eval(input())) for _ in range(A)] t = [int(eval(input())) for _ in range(B)] x = [int(eval(input())) for _ in range(Q)] def BinaryIndexSearch_floor(p, SL): """ search the minimum number among the larger numbers than the ginven nymber "n". return the index of it under the list sorted. """ #SL = sorted(L) l = 0 r = len(SL) - 1 while True: if l == r: if SL[l] > p: return l else: return -1 elif r-l == 1: if SL[l] > p: return l elif SL[r] > p: return r else: return -1 center = l + ((r-l) // 2) if SL[center] <= p: l = center + 1 continue else: r = center continue def BinaryIndexSearch_ceil(p, SL): #SL = sorted(L) l = 0 r = len(SL) - 1 while True: if l == r: if SL[l] < p: return l else: return -1 elif r-l == 1: if SL[r] < p: return r elif SL[l] < p: return l else: return -1 center = l + ((r-l) // 2) if SL[center] < p: l = center continue else: r = center - 1 continue inf = float("inf") for i in range(Q): xx = x[i] I_s_r = BinaryIndexSearch_floor(xx, s) I_t_r = BinaryIndexSearch_floor(xx, t) I_s_l = BinaryIndexSearch_ceil(xx, s) I_t_l = BinaryIndexSearch_ceil(xx, t) s_r = inf if I_s_r == -1 else s[I_s_r] - xx t_r = inf if I_t_r == -1 else t[I_t_r] - xx s_l = inf if I_s_l == -1 else xx - s[I_s_l] t_l = inf if I_t_l == -1 else xx - t[I_t_l] ans = [] ans.append(min(s_r,t_l) + s_r + t_l) ans.append(min(t_r,s_l) + t_r + s_l) ans.append(max(s_r,t_r)) ans.append(max(s_l,t_l)) print((min(ans)))
A, B, Q = list(map(int, input().split())) s = [int(eval(input())) for _ in range(A)] t = [int(eval(input())) for _ in range(B)] x = [int(eval(input())) for _ in range(Q)] def BinaryIndexSearch_floor(p, SL): """ search the minimum number among the larger numbers than the ginven nymber "n". return the index of it under the list sorted. """ #SL = sorted(L) l = 0 r = len(SL) - 1 while True: if l == r: if SL[l] > p: return l else: return -1 elif r-l == 1: if SL[l] > p: return l elif SL[r] > p: return r else: return -1 center = l + ((r-l) // 2) if SL[center] <= p: l = center + 1 continue else: r = center continue def BinaryIndexSearch_ceil(p, SL): #SL = sorted(L) l = 0 r = len(SL) - 1 while True: if l == r: if SL[l] < p: return l else: return -1 elif r-l == 1: if SL[r] < p: return r elif SL[l] < p: return l else: return -1 center = l + ((r-l) // 2) if SL[center] < p: l = center continue else: r = center - 1 continue inf = float("inf") for i in range(Q): xx = x[i] I_s_r = BinaryIndexSearch_floor(xx, s) I_t_r = BinaryIndexSearch_floor(xx, t) I_s_l = A-1 if I_s_r == -1 else I_s_r - 1 I_t_l = B-1 if I_t_r == -1 else I_t_r - 1 s_r = inf if I_s_r == -1 else s[I_s_r] - xx t_r = inf if I_t_r == -1 else t[I_t_r] - xx s_l = inf if I_s_l == -1 else xx - s[I_s_l] t_l = inf if I_t_l == -1 else xx - t[I_t_l] ans = [] ans.append(min(s_r,t_l) + s_r + t_l) ans.append(min(t_r,s_l) + t_r + s_l) ans.append(max(s_r,t_r)) ans.append(max(s_l,t_l)) print((min(ans)))
p03112
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(eval(input())) for i in range(1,B+1): t[i] = int(eval(input())) for i in range(Q): X[i] = int(eval(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)
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(eval(input())) for i in range(1,B+1): t[i] = int(eval(input())) for i in range(Q): X[i] = int(eval(input())) import bisect for x in X: s_i = bisect.bisect_left(s,x) t_i = bisect.bisect_left(t,x) s_l = abs(s[s_i-1]-x) s_r = s[s_i]-x t_l = abs(t[t_i-1]-x) t_r = t[t_i]-x ans = min([max(s_l,t_l),s_l+t_r+min(s_l,t_r),s_r+t_l+min(s_r,t_l),max(s_r,t_r)]) print(ans)
p03112
import bisect A,B,Q=list(map(int,input().split())) S=[-10**18]+[int(eval(input())) for _ in range(A)]+[10**18] T=[-10**18]+[int(eval(input())) for _ in range(B)]+[10**18] for i in range(Q): x=int(eval(input())) b,d=bisect.bisect_right(S,x), bisect.bisect_right(T,x) ans=float("inf") for s in [S[b-1], S[b]]: for t in [T[d-1], T[d]]: d1,d2=abs(s-x)+abs(t-s), abs(t-x)+abs(s-t) ans=min(ans,d1,d2) print(ans)
import bisect INF=10**18 A,B,Q=list(map(int,input().split())) S=[0]*(A+2) T=[0]*(B+2) S[0],T[0]=-INF,-INF S[A+1],T[B+1]=INF,INF for i in range(1,A+1): S[i]=int(eval(input())) for i in range(1,B+1): T[i]=int(eval(input())) for i in range(Q): x=int(eval(input())) b,d=bisect.bisect_right(S,x), bisect.bisect_right(T,x) ans=INF for s in [S[b-1],S[b]]: for t in [T[d-1],T[d]]: d1,d2=abs(s-x)+abs(t-s),abs(t-x)+abs(s-t) ans=min(ans,d1,d2) print(ans)
p03112
import bisect import sys a, b, q = list(map(int, input().split())) lines = sys.stdin.readlines() sss = list(map(int, lines[:a])) ttt = list(map(int, lines[a:a + b])) xxx = list(map(int, lines[a + b:])) nearest_temples = [0] * a nearest_shrines = [0] * b for i, s in enumerate(sss): ti = bisect.bisect(ttt, s) nt = 10 ** 11 if ti > 0: nt = min(nt, abs(s - ttt[ti - 1])) if ti < b: nt = min(nt, abs(s - ttt[ti])) nearest_temples[i] = nt for i, t in enumerate(ttt): si = bisect.bisect(sss, t) ns = 10 ** 11 if si > 0: ns = min(ns, abs(t - sss[si - 1])) if si < a: ns = min(ns, abs(t - sss[si])) nearest_shrines[i] = ns buf = [] for x in xxx: si = bisect.bisect(sss, x) ti = bisect.bisect(ttt, x) ans = 10 ** 11 if si > 0: ans = min(ans, x - sss[si - 1] + nearest_temples[si - 1]) if si < a: ans = min(ans, sss[si] - x + nearest_temples[si]) if ti > 0: ans = min(ans, x - ttt[ti - 1] + nearest_shrines[ti - 1]) if ti < b: ans = min(ans, ttt[ti] - x + nearest_shrines[ti]) buf.append(ans) print(('\n'.join(map(str, buf))))
import bisect import sys SENTINEL = 10 ** 12 a, b, q = list(map(int, input().split())) lines = sys.stdin.readlines() sss = [-SENTINEL] + list(map(int, lines[:a])) + [SENTINEL] ttt = [-SENTINEL] + list(map(int, lines[a:a + b])) + [SENTINEL] xxx = list(map(int, lines[a + b:])) nearest_temples = [SENTINEL] + [0] * a + [SENTINEL] nearest_shrines = [SENTINEL] + [0] * b + [SENTINEL] for i, s in enumerate(sss[1:-1], start=1): ti = bisect.bisect(ttt, s) nearest_temples[i] = min(ttt[ti] - s, s - ttt[ti - 1]) for i, t in enumerate(ttt[1:-1], start=1): si = bisect.bisect(sss, t) nearest_shrines[i] = min(sss[si] - t, t - sss[si - 1]) buf = [] for x in xxx: si = bisect.bisect(sss, x) ti = bisect.bisect(ttt, x) ans = min(x - sss[si - 1] + nearest_temples[si - 1], sss[si] - x + nearest_temples[si], x - ttt[ti - 1] + nearest_shrines[ti - 1], ttt[ti] - x + nearest_shrines[ti]) buf.append(ans) print(('\n'.join(map(str, buf))))
p03112
a, b, q = list(map(int, input().split())) s = [] t = [] x = [] for i in range(a): s.append(int(eval(input()))) for i in range(b): t.append(int(eval(input()))) for i in range(q): x.append(int(eval(input()))) import bisect def get(x): ls,rs,lt,rt=10**13,10**13,10**13,10**13 s_ind = bisect.bisect_left(s,x) t_ind = bisect.bisect_left(t,x) if s_ind!=0: ls=abs(x - s[s_ind-1]) if s_ind!=a: rs=abs(x - s[s_ind]) if t_ind!=0: lt=abs(x - t[t_ind-1]) if t_ind!=b: rt=abs(x - t[t_ind]) return ls,rs,lt,rt for que in x: ls,rs,lt,rt = get(que) rr = max(rs,rt) ll = max(ls,lt) lsrt = 2*ls+rt ltrs = 2*lt+rs rslt = 2*rs+lt rtls = 2*rt+ls ans = min([rr,ll,lsrt,ltrs,rslt,rtls]) print(ans)
import sys input=sys.stdin.readline a, b, q = list(map(int, input().split())) s = [] t = [] x = [] for i in range(a): s.append(int(eval(input()))) for i in range(b): t.append(int(eval(input()))) for i in range(q): x.append(int(eval(input()))) import bisect def get(x): ls,rs,lt,rt=10**13,10**13,10**13,10**13 s_ind = bisect.bisect_left(s,x) t_ind = bisect.bisect_left(t,x) if s_ind!=0: ls=abs(x - s[s_ind-1]) if s_ind!=a: rs=abs(x - s[s_ind]) if t_ind!=0: lt=abs(x - t[t_ind-1]) if t_ind!=b: rt=abs(x - t[t_ind]) return ls,rs,lt,rt for que in x: ls,rs,lt,rt = get(que) rr = max(rs,rt) ll = max(ls,lt) lsrt = 2*ls+rt ltrs = 2*lt+rs rslt = 2*rs+lt rtls = 2*rt+ls ans = min([rr,ll,lsrt,ltrs,rslt,rtls]) print(ans)
p03112